...

View Full Version : search and replace



drewamnh
07-11-2002, 08:16 PM
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.....

joh6nn
07-11-2002, 08:48 PM
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?

drewamnh
07-11-2002, 08:58 PM
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.

joh6nn
07-11-2002, 09:08 PM
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.

jkd
07-11-2002, 11:34 PM
I'd try:

document.body.innerHTML = document.body.innerHTML.replace(RegExp('you are right','gi'), '<img src="right.gif"/>');

joh6nn
07-11-2002, 11:41 PM
nice one.

drewamnh
07-12-2002, 04:10 PM
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>

jkd
07-12-2002, 04:50 PM
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>

It works in IE4+/NS6+.

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...

drewamnh
07-12-2002, 04:55 PM
ahhh....

I used to work in a Microsoft world- but now I work at a museum which is all open source freeware uukkk...

how I miss the good old asp days...



:(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum