www.webdeveloper.com
Results 1 to 3 of 3

Thread: Link Properties

  1. #1
    Join Date
    Dec 2006
    Posts
    76

    Link Properties

    Does someone know how to write a code that will find out what the web address is of any given link you click on in a website? Any help is much appreciated!

  2. #2
    Join Date
    Dec 2006
    Location
    Scotland
    Posts
    450
    Hmmm... yes, but there are limitations, as I'll explain afterwards.

    Code:
    onload = function() {
     document.onclick = function(event) { alertNavigation(event) }
    }
    
    function alertNavigation(e) {
     var evt = e||event;
     var evtTagName = (evt.srcElement||evt.target).tagName;
     if (evtTagName&&evtTagName.toLowerCase()=="a")
      alert(evt.srcElement.href);
    }
    That code sets a simple onclick handler to the document, detects if the element clicked was an <a> tag and if so, alert out the href attribute. You might want to expand that to deal with anything that is setting the location.href property via script as that is also a common method of navigation (although semantically incorrect, imo).

    Now the limitations I mentioned are that you can't try and use this on a document not on the domain it's being called on. For example, if you have an iframe on your page and it's pointing to http://www.anotherwebsite.com/somepage.html then you cannot set and detect the links clicked in there.

  3. #3
    Join Date
    Dec 2006
    Posts
    76
    Thank you for your reply. I have modified/added to your script so that it will not show the address in a popup window and it will open the address in a new window. See below:

    onload = function() {
    document.onclick = function(event) { alertNavigation(event) }
    }

    function alertNavigation(e) {
    var evt = e||event;
    var evtTagName = (evt.srcElement||evt.target).tagName;
    if (evtTagName&&evtTagName.toLowerCase()=="a")
    window.open(evt.srcElement, evt.target).tagName
    }


    My question now is, how do I keep the original window from going to the linked address and yet have the new window still go to it?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles