I will answer the question in the update about events in IE:
function track_file(evt) { if (evt == undefined) { evt = window.event;
is a classic way to get a consistent event object in browsers.
After that, I will use regular expressions to normalize the URL, but I'm not sure what you need.
[EDIT] Some real code to put into practice what I wrote above ... :-)
function CheckTarget(evt) { if (evt == undefined) { // For IE evt = window.event; //~ event.returnValue = false; var target = evt.srcElement; var console = { log: alert }; } else { target = evt.target; //~ preventDefault(); } alert(target.hostname + " vs. " + window.location.hostname); var re = /^https?:\/\/[\w.-]*?([\w-]+\.[az]+)\/.*$/; var strippedURL = window.location.href.match(re); if (strippedURL == null) { // Oops! (?) alert("Where are we?"); return false; } alert(window.location.href + " => " + strippedURL); var strippedTarget = target.href.match(re); if (strippedTarget == null) { // Oops! (?) alert("What is it?"); return false; } alert(target + " => " + strippedTarget); if (strippedURL[1] == strippedTarget[1]) { //~ window.location.href = target.href; // Go there return true; // Accept the jump } return false; }
This test code, not production code, is obvious!
The lines with // ~ comments show an alternative way to prevent the user from clicking on the link to go. This is somehow more efficient, because if I use Firebug console.log, it is curious that return false is inefficient.
I used the "follow the link or not" behavior here, not knowing the real ultimate goal.
As pointed out in the comments, RE may be easier using the hostname instead of href ... I leave it because it is already encoded and may be useful in other cases.
In both cases, special precautions must be taken to handle special cases, such as localhost, IP addresses, ports ...
I got rid of the domain name before re-reading the question and see that this is not a problem ... Well, maybe this can be useful to someone else.
Note. I showed a similar solution in the issue of decorating links: Editing all external links using javascript
Philho
source share