2

Is it possible to catch the attribute of hyperlink with JavaScript or Jquery on browsers open new tab event?

lets say I have a hyperlink <a href="http://example.com/something">MyLink</a> and using right click on link and opening new tab should alert first "http://example.com"

sharptooth
  • 167,383
  • 100
  • 513
  • 979
fefe
  • 8,755
  • 27
  • 104
  • 180
  • 1
    I'm rather certain that you can't do this. Perhaps if you explain your need for such a thing then we can help you figure out a work-around for your scenario? – Grinn Jan 31 '13 at 18:31
  • I have a tracking tool which get's the top level domain on hyperlink click when it's happening on site and it's working good. I would like to extend the functionality of this. And one of my collegue was insisting that is possible to make with JavaScript I was skeptic and I said to myself I'm gonna make some researches – fefe Jan 31 '13 at 18:39

2 Answers2

2

I think this is only possible with writing a browser extension. You cant control this type of things with JavaScript.

gotohales
  • 3,665
  • 1
  • 20
  • 34
Soroush Falahati
  • 2,196
  • 1
  • 27
  • 38
2

Ok, so this is the best that I can come up with but it ain't pretty.

Have all the links on the page point to another page (we'll call it redir.html). redir.html is passed a parameter via the URL that defines what page it should then open, like redir.html?page=www.example.com/something. redir.html then executes whatever JavaScript you want, perhaps only if it was opened in a new tab/window. This can be detected by checking to see if window.history.length === 1.

redir.html:

<script>
function getParameter(param) {
            var val = document.URL;
            var url = val.substr(val.indexOf(param))  
            var n=url.replace(param+"=","");
            return n;
}
var page = getParameter("page");
if(window.history.length === 1)
{
    alert("I opened in a new tab or window");
}

window.location.href = page;
</script>

Example anchor tag on source page:

<a href="redir.html?page=http://example.com/something">My link</a>

(I got the getParameter function from this SO post.)

Community
  • 1
  • 1
Grinn
  • 5,370
  • 38
  • 51
  • @fefe, can you explain what you mean by that? – Grinn Jan 31 '13 at 20:10
  • If you're referring to https to http redirects, perhaps you could change `` to ``. – Grinn Jan 31 '13 at 20:14
  • I made a html document and I've been packing the codes above. then calling http://localhost/test.html I won't get to the target page. is going to produce a 404 error page I have been trying to replace the top level domains protocol as well – fefe Jan 31 '13 at 20:33
  • n is returning every time / – fefe Jan 31 '13 at 20:50
  • It sounds like perhaps you didn't include the `?page=www.example.com` at the end of your link. Are you certain that you read the suggestion carefully? It's not that you'd add this code to your page *containing* the link, it's that you would link to a newly created page containing this code. – Grinn Jan 31 '13 at 20:53
  • yah man thats it!! it should work. I have to test with dynamical contents. The concept is cool! – fefe Jan 31 '13 at 21:39