I need a javascript that reloads a page and replaces a string, say a unique div tag or something, and replace it with some other tag.... help.....
Announcement
Collapse
No announcement yet.
search and replace
Collapse
X
-
i think you have to be a little bit more specific, with exactly what it is that you're looking for, before we can help you. maybe an example?
-
Example
I'm not allowed to touch the cgi script which generates the page to begin with. so I need a javascript function that will look for the string "you are right" and replace it with an image <img src="right.gif"/>
perhaps using an array since their will be several questions on the page...
I hope that makes sense.
Comment
-
eww. that could be tricky. if the phrase you're looking for isn't stand alone, and inside of some sort of object with an id, like a DIV or SPAN, you're out of luck, with javascript. if it's inside of a DIV, though, i think i can help.
Comment
-
I'd try:
document.body.innerHTML = document.body.innerHTML.replace(RegExp('you are right','gi'), '<img src="right.gif"/>');
Comment
-
reply
your solution is not cross browser - but thank you. I thinked I found the solution:
function replace_image(key) {
if(key == 'you are right'){
document.write('<img src="yes.gif" alt="<-- you are right!" \/>');
}else{
document.write('<img src="no.gif" alt="<-- you are wrong!" \/>');
}
}
</script>
</head>
<body>
<script language="JavaScript">
replace_image('%answer%')
</script>
</body>
Comment
-
Re: reply
Originally posted by drewamnh
your solution is not cross browser - but thank you. I thinked I found the solution:
function replace_image(key) {
if(key == 'you are right'){
document.write('<img src="yes.gif" alt="<-- you are right!" \/>');
}else{
document.write('<img src="no.gif" alt="<-- you are wrong!" \/>');
}
}
</script>
</head>
<body>
<script language="JavaScript">
replace_image('%answer%')
</script>
</body>
Anyway, is %answer% some ASP variable or something? Otherwise what you just posted won't do anything in a purely client-side setting.
BTW, next time you have ASP support - let us know.
Much better solutions are possible...
Comment
Comment