Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Old 03-19-2009, 08:11 PM javascript replace
Extreme Talker

Posts: 176
Trades: 0
I want to replace text on the document with javascript

like where it finds "word" it will replace it with "cow"


is this possible? I've searched but can't find anything.
Skeddles is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-19-2009, 08:16 PM Re: javascript replace
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.w3schools.com/jsref/jsref_replace.asp
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-19-2009, 08:46 PM Re: javascript replace
Extreme Talker

Posts: 176
Trades: 0
For some reason my page keeps continuously loading

Code:
<html>
<head>
<script type="text/javascript">
function replacetop() {
var topbefore="before";
var topafter="newfore";

document.write(document.body.innerHTML.replace(topbefore,topafter));
}

function replacebottom() {
var bottombefore="after";
var bottomafter="aftore";

document.write(document.body.innerHTML.replace(bottombefore,bottomafter));
}
</script>
</head>
<body>




<form>
<input type="button" onclick="replacetop()" value="Call function">
<input type="button" onclick="replacebottom()" value="Call function">
</form>

before ME after

</body>
</html>
any idea?

http://linkpagecentral.com/js.php

Last edited by Skeddles; 03-19-2009 at 09:06 PM..
Skeddles is offline
Reply With Quote
View Public Profile
 
Old 03-20-2009, 04:15 AM Re: javascript replace
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,383
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
It's because you are running it as a document.write in the head, doing that will replace the whole document with whatever you write back.

load the body innerHTML into a string, replace and put the text back in to the body.
eg:
Code:
function before() {
var m_reBefore= /before/g;
var m_sAfter="after";
var m_sStr = document.body.innerHTML.replace(m_reBefore,m_sAfter);
document.body.innerHTML = m_sStr;
}
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to javascript replace
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13561 seconds with 12 queries