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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    string match help

    I'm trying to create a script where all the hyperlinks in a page will be replaced with a new set of coding before it. (e.g: a href="this.htm?http://www.google.com").

    My code generates this change through a series of random numbers and only runs the script when "2" is the generated number (out of 12). Now, I only know how to replace all occurrences of "href" in all "a" tags, but there are times when I use Javascript to launch new windows or even javascript to navigate to the top of a page.

    I tried to use a string match method to search for occurrences of "javascript:" and so forth, but I can't seem to get it working.

    Here's what I got so far:
    Code:
      function loadInterstitial() {
        var ranNum     =  Math.round(Math.random()*11);
        var adUrl     =  "ad.htm";
    
        if (ranNum == 2) {
          var links = document.getElementsByTagName("a");
    
          for (var i=0; i<links.length; i++) {
            var redir  = links[i].href;
            var JScript = links.match("javascript:");
    
            if (JScript = true) {
              links[i].href = redir;
            } else {
            links[i].href = adUrl + "?redir=" + redir + "&timer=3000";
          }
        } else if (ranNum != 2) {
          return ranNum;
        }
      }
    I'm trying to get a page to load in between browsing (such as an interstitial, only not as annoying). I can make it appear for every link fine and randomize it as I please, but I can't seem to get this part to work.

    Problem: I don't want the script to load the new url when I'm using javascript to launch new windows in "a" tags. (e.g: a href="javascript:scroll(0,0)" is changed to a href="ad.htm?javascript:scroll(0,0)"). Is there any way around this?

    I tried using external Javascript files to write out the code "a href='javascript:scroll(0,0)", but this script still detects it and writes the code before this link. I don't want to use IFRAMEs, because they are hard to manipulate with my site.

    Solution, please?

  • #2
    New Coder
    Join Date
    Dec 2005
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The reason your code wont work is because you have:

    if (JScript = true)

    That is assigning the value of "true" to JScript.It should be:

    if (JScript == true)

    Another way to do it is to give your HREFs classes. Then inside your script check the class of the HREF chosen. If it is one that you don't want to change then do nothing. If it isn't then do whatever you want.
    Code:
    function loadInterstitial() {
        var ranNum     =  Math.round(Math.random()*11);
        var adUrl     =  "ad.htm";
    
        if (ranNum == 2) {
          var links = document.getElementsByTagName("a");
    
          for (var i=0; i<links.length; i++) {
            var redir  = links[i].href;
            if (links[i].className == "JS") {
              links[i].href = redir;
            } else {
            links[i].href = adUrl + "?redir=" + redir + "&timer=3000";
          }
        } else if (ranNum != 2) {
          return ranNum;
        }
      }

  • #3
    New Coder
    Join Date
    Nov 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay, I tried it the first method by setting it to "== true", but that didn't work. Instead, I set it as a class name in order to achieve the best effect. And it worked!

    Thank you very much for your speedy help.

  • #4
    New Coder
    Join Date
    Dec 2005
    Posts
    88
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glad to have been of help.


  •  

    Posting Permissions

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