Changing Script

Discussion in 'Javascript' started by Jarry, Dec 21, 2006.

  1. Jarry

    Jarry Guest

    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?
     
    Jarry, Dec 21, 2006
    #1
    1. Advertising

  2. Jarry

    RobG Guest

    Jarry wrote:
    > 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/#
    >



    --
    Rob
     
    RobG, Dec 22, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samridhi Kumar Shukla
    Replies:
    1
    Views:
    527
    Alvin Bruney
    Nov 30, 2003
  2. Aaron Beall
    Replies:
    2
    Views:
    1,829
    Aaron Beall
    Sep 14, 2007
  3. Rajat
    Replies:
    3
    Views:
    712
    Jorgen Grahn
    Jan 8, 2010
  4. VYAS ASHISH M-NTB837
    Replies:
    2
    Views:
    572
    Jan Kaliszewski
    Jan 7, 2010
  5. VK
    Replies:
    3
    Views:
    182
    Dr J R Stockton
    Mar 15, 2007
Loading...

Share This Page