Changing Script

J

Jarry

If I take a website that has body text and <script> tags, I can do:

document.body.innerHTML = document.body.innerHTML.replace("no","yes");

but if i want to change the script as such

document.body.innerHTML =
document.body.innerHTML.replace("alert\(\"no\"\)","alert\(\"yes\"\)");

I can't - how can I get to the section with the javascript in it?
 
R

RobG

Jarry said:
If I take a website that has body text and <script> tags, I can do:

document.body.innerHTML = document.body.innerHTML.replace("no","yes");

but if i want to change the script as such

document.body.innerHTML =
document.body.innerHTML.replace("alert\(\"no\"\)","alert\(\"yes\"\)");

I can't - how can I get to the section with the javascript in it?

The short answer is: for all practical purposes, you can't.

innerHTML is a non-standard property of DOM elements introduced by
Microsoft in IE. It has been widely copied by other browsers to the
extent that it is a /de facto/ standard, however, there is no public
specification for it and implementations vary.

It should only be used with simple HTML to create fully-formed DOM
objects that replace or are appended to existing DOM elements.
Replacing an element's innerHTML property does not cause code within
script elements to be executed, although some libraries include methods
to do so[1].

You might employ those methods for your attempted replacement trick,
however unless scripts are written to be executed that way, results
will be unpredictable.


1. Script elements are collected using a regular expression and their
content eval'd. A fairly crude but reliable mechanism that has some
foibles for the unwary.

Try this thread:
<URL:
http://groups.google.com.au/group/comp.lang.javascript/browse_frm/thread/7e23f42490c301de/#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top