Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts

    search and replace

    I'm building a new site that takes data from external sources, but we dont want large load times so I'm using XMLhttpRequest objects to get the data after the page is loaded, which is all fine but I dont want to create 2 versions of the site, so I wondered if it was possible to search for specific text in the page and replace it - ie look for the links to the no-script versioned pages and replace them with links to the 'hi tech' version.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Not very clear to me... First you say text than you say link. From the nodeType point of view, it is not the same thing.

    Now, yes, you may find/refere/replace everything on the page, using various DOM methods, but you must be more specific.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    ok I want to replace links to accom.php to accomx.php

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    this should work, if I well understood what you want to replace:

    Code:
    <script type="text/javascript">
    onload = function(){
    var aa=document.getElementsByTagName('a');
    for(var i=0;i<aa.length;i++){
    aa[i].href&&aa[i].href.match(/accom.php/)?aa[i].href=aa[i].href.replace(/accom.php/g,'accomx.php'):null;
    }
    }
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Quote Originally Posted by Kor
    this should work, if I well understood what you want to replace:

    Code:
    <script type="text/javascript">
    onload = function(){
    var aa=document.getElementsByTagName('a');
    for(var i=0;i<aa.length;i++){
    aa[i].href&&aa[i].href.match(/accom.php/)?aa[i].href=aa[i].href.replace(/accom.php/g,'accomx.php'):null;
    }
    }
    </script>
    OK, just because I like to know what stuff does before I paste it in Mind telling me if this interpretation is correction

    onload = function(){
    //I'm assuming that this is instead of <body onload="function()" - is this a new thing, I dont keep up to date with javascript but if this works cross browser, it seems strange to me that people havent been doing it for years...

    var aa=document.getElementsByTagName('a');
    // local variable aa = array of link elements
    for(var i=0;i<aa.length;i++){
    //for each link element
    aa[i].href&&aa[i].href.match(/accom.php/)?aa[i].href=aa[i].href.replace(/accom.php/g,'accomx.php'):null;
    //if the link element has an href and the href = accom.php then change the href to accomx.php, otherwise leave it alone.
    }
    }
    [/code]
    Is that right? Not sure about the replace, why doesnt the first term have quotes around it and why does it end in /g?

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Correct. Now about the replace... I have used RegularExpression

    string.match(/substring/)
    tries to find if the substring is anywere inside the string. If so:

    string = string.replace(/substring/g,'anothersubstring')
    replaces the subtring whereever it might be (g is a RegExp flag and matches global)

    The /.../ container makes the expresion to be a Regular Expression. It is a shortcut for

    new RegExp('substring')

    object
    Last edited by Kor; 04-29-2006 at 08:35 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    cheers


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •