SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Kill a href

  1. #1
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Kill a href

    How do I change an "a" with a href to one without.

    I use alot of anchors to scroll divisions when js is turned off.
    What I would like to do is destroy these hrefs and use a javascript function instead.( turn a "<a href>" to a "<a>" )
    At the moment the back button includes the anchors, which is abit annoying.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A legitimate use of the javascript: url (which you tend to see sprinkled around scripts for no apparent reason along with eval()):
    Code:
    window.onload=function()
    {
    	var anchors = document.getElementsByTagName("a");
    	for(var i = 0, len = anchors.length; i < len; i++)
    	{
    		anchors[i].href = "javascript:void 0"
    	}
    }
    The void operator returns undefined no matter what it's operand is. You could use undefined in place of void 0, but the undefined keyword was only added to javascript in later versions.

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    what I'm trying to do is kill the href completely, so there is no "href" at all.
    I've tried things like these, which don't work
    Code:
    anchors[i].href=null
    anchors[i].setAttribute("href",null)
    anchors[i].clearAttribute("href") >>>> wild guess....lol
    but I think you can see what I'm trying to accomplish
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does it matter if there is an href or not as long as it doesn't do anything?

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here ya go. This means the <a> won't be underlined or highlighted:
    Code:
    window.onload=function()
    {
    	var anchors = document.getElementsByTagName("a");
    	for(var i = 0, len = anchors.length; i < len; i++)
    	{
    		anchors[i].removeAttribute("href");
    	}
    }

  6. #6
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It might be worth noting that the original code, with the href, is what will display in the source view, but the attribute will be removed from the actual document object, as this demonstrates:

    <a href="#" onclick="alert('Apply removeAttribute to:\n'+this.href);
    this.removeAttribute('href'); //comment out or delete this line to see change
    alert(this.href? 'href here' : 'no href');
    return false;">
    test href
    </a>


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
  •