Results 1 to 2 of 2

Thread: howto: automatically change the href target if href is #

  1. #1
    Join Date
    Aug 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question howto: automatically change the href target if href is #

    Hi All,

    I am looking for a simple javascript that i can had to a page that will automatically search all of the links on a page and if it finds any that are
    Code:
    <a href="#">anylink</a>
    will add target="_self" these specific links so that they become
    Code:
    <a href="#" target="_self">anylink</a>
    Basically all i want to do is add the target reference on for hash links.

    Hope that makes sense...

    Any and all help very appreciated!

    Cheers,
    Darren

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function addEvent(el, ev, f) {
      if(el.addEventListener)
        el.addEventListener(ev, f, false);
      else if(el.attachEvent)
        el.attachEvent("on" + ev, f);
      else
        el['on' + ev] = f;
    }
    
    addEvent(
      window,
      "load",
      function() {
        for(var i = 0, e = document.links || document.getElementsByTagName("a"); i < e.length; ++i)
          if(e[i].href && e[i].href === "#")
            e[i].target = "_self";
      }
    );
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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