Alternative method to var page = document.body.innerHTML=document.body.innerHTML=page

Discussion in 'JavaScript' started by MyVodaFone, Sep 22, 2010.

  1. #1
    I looking for an alternative to the code below, it works fine, but it kills other javascripts running on the page, I guess I'm redeclaring the page or something so do I have any other options, beside getelementbyid, its just random text forund anywhere on the page

    
    var page = document.body.innerHTML=document.body.innerHTML=page.replace(/this/g, 'with this');
    Code (markup):
    EDIT: emm, document.body is only between the <body tags, what should I be using to include the <head
     
    Last edited: Sep 22, 2010
    MyVodaFone, Sep 22, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Doing a global search and replace on the entire document.body is likely to be an intensive process because you're often dealing with (essentially) a very large string. Every insertion will require reconstructing the document.body contents. It would be better to copy document.body into a variable, do your search and replace on that data, and then set document.body equal to that variable. It would be even better if you could limit the process to a given section of your document - a specific <DIV>, for example. It would be best if you could do this server-side before sending the document to the user. Good luck!
     
    rainborick, Sep 22, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    It doesn't run on a website, its running through the browser... its part of a firefox addon. The majority of sites are fine, but when you load DP for example it has a lot of java going on and the above doesn't return header scripts, so thats what I'm looking for.
     
    MyVodaFone, Sep 22, 2010 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Maybe try:
    
    document.getElementsByTagName("html")[0].innerHTML
    
    Code (markup):
    or
    
    document.getElementsByTagName("head")[0].innerHTML
    
    Code (markup):
    Is the idea of this script to modify the documents HTML or to store it within the variable 'page' ??
     
    camjohnson95, Sep 22, 2010 IP
  5. GFX_Lover

    GFX_Lover Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I hope this works,

    (d = document.body).innerHTML = d.innerHTML.replace(/this/gi, "with this");
     
    GFX_Lover, Oct 3, 2010 IP