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 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    detect a href URL in page and assign function to it

    hi, very odd one here and im not sure if it can be done and if so not sure where to start.

    I have 2 links <a href="http://www.aaa.co.uk">a</a> and <a href="http://www.bbb.co.uk">b</a> for example.

    without having the onlick function, is it possible to check the value of the a href for "aaa" and carry out a function like open newWindow, alert, anything and only do it for that type of URL, where as "bbb" would just function as normal. So its like a URL checker / validator changer.

    cheers - Death = $Mill
    Last edited by deathmill; 07-10-2005 at 01:45 PM.

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script type="text/javascript">
    function updateLinks(){
    links=document.getElementsByTagName('a');
    for (i = 0; i < links.length; i++)
    if (links[i].href.indexOf('aaa')!==-1)
    links[i].onclick=function(){
    window.open(this.href); // <<< Do whatever on the other side of the comment (//) marks
    return false;
    }
    }
    window.onload=updateLinks;
    </script>

  • #3
    New Coder
    Join Date
    Oct 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    detect a href URL in page and assign function to it

    oh my gard! it can be done, thank you so so much, one things just popped into my head, is it possible to add some kind of sleep function, say for example delay the popup for 5 seconds or something once I click the aaa link.

    Im so happy, your the man jscheuer1 !

    Death = $Mill

    Quote Originally Posted by jscheuer1
    Code:
    <script type="text/javascript">
    function updateLinks(){
    links=document.getElementsByTagName('a');
    for (i = 0; i < links.length; i++)
    if (links[i].href.indexOf('aaa')!==-1)
    links[i].onclick=function(){
    window.open(this.href); // <<< Do whatever on the other side of the comment (//) marks
    return false;
    }
    }
    window.onload=updateLinks;
    </script>

  • #4
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Technically, yes. In practice, some browsers may see that as a pop up add and block it. All you can do is try and see what happens, use:
    Code:
    setTimeout("window.open(this.href)",5000);
    in place of:
    Code:
    window.open(this.href);

  • #5
    New Coder
    Join Date
    Oct 2002
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'v been playing around with the setTimeOut function and I keep getting a url "undefined" error, doesn't look as if the "this.href" is picking up the URL from the a href when delayed using setTimeOut.

    any other throughts.

    D

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Is there a typo at this line


    if (links[i].href.indexOf('aaa')!==-1)


    should it not be

    if (links[i].href.indexOf('aaa')!=-1)

    As for your timeout try this

    PHP Code:
    <script type="text/javascript">
    <!--
    function 
    updateLinks(){
    links=document.getElementsByTagName('a');
    for (
    0links.lengthi++){

    if (
    links[i].href.indexOf('aaa')!=-1){

    temp=links[i].href

    links
    [i].onclick=function(){
    setTimeout("window.open(temp)",5000); // <<< Do whatever on the other side of the comment (//) marks
    return false;
    }

    }

    }
    }
    window.onload=updateLinks;

    //-->
    </script> 
    Last edited by Mr J; 07-10-2005 at 09:37 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah, I hate when that happens, computers cannot do anything other than what we tell them and sometimes we have no idea what we are telling them. This worked in IE6 locally:
    Code:
    <html>
    <head>
    <title>Update Link - Demo</title>
    <script type="text/javascript">
    function updateLinks(){
    var funcString
    links=document.getElementsByTagName('a');
    for (i = 0; i < links.length; i++)
    if (links[i].href.indexOf('hoo')!==-1){
    funcString='setTimeout("window.open(\''+links[i].href+'\')",5000);return false;'
    links[i].onclick=new Function(funcString)
    }
    }
    window.onload=updateLinks;
    </script>
    </head>
    <body>
    <a href="http://www.yahoo.com">Yahoo</a>
    </body>
    </html>

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can also use anonymous function in setTimeout/setInterval.

    setTimeout(function(){window.open(this.href)}, 5000);
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    You can also use anonymous function in setTimeout/setInterval.
    Code:
    setTimeout(function(){window.open(this.href)}, 5000);
    Not in this case, as 'this.href' will not resolve as expected when the link is clicked.

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can put this.href in a local variable inside the onclick handler and then access that inside the setTimeout anonymous function.
    Code:
    <html>
    <head>
    <title>Update Link - Demo</title>
    <script type="text/javascript">
    function updateLinks(){
      var url;
      var links=document.getElementsByTagName('a');
      for (var i = 0; i < links.length; i++){
        if (links[i].href.indexOf('.com')!=-1){
          links[i].onclick=function(){
            url = this.href;
            setTimeout(function(){window.open(url)}, 5000);
            return false;
          }
        }
      }
    }
    window.onload=updateLinks;
    </script>
    </head>
    <body>
    <a href="http://www.yahoo.com">Yahoo</a>
    <a href="http://www.google.com">Google</a>
    </body>
    </html>
    Last edited by glenngv; 07-11-2005 at 11:06 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    Regular Coder
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    375
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then the big question is: Is that better, worse or the same, for whatever reason(s), as putting the literal url into the function while writing it, as I did? Looks a little easier on the eyes than the escaped quotes mine required. It creates more variables at the same level of scope (assuming more than one link gets used), I think. Mine keeps using the same variable over until all the onclicks are written. I'm sure for something like this it is unimportant but, it's interesting to me as I go on coding other scripts.

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    For this situation, it might not make a big difference. But here are the advantages of using anonymous functions over the Function constructor and string expression in setTimeout/setInterval:
    1. Easily maintained. If in the future you want to add more functionality other than opening a new window, you can easily modify it.
    2. No confusing mixture of single and double quotes.
    3. You can pass objects as parameters and not only strings/numbers.
    4. The Function constructor and string expression in setTimeout/setInterval are slower than anonymous functions as they are similar to eval.

    Of course, it has also disadvantages as using anonymous functions are prone to IE memory leaks. Even my posted code seems to fall on that IE bug. Glad there is a fix for that.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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