The task is weird but it has some reason:
All external URLs should be opened in new windows with some JS confirm (I don’t speak german so have no idea what real confirm text is).

So the idea is simple and transparent: Add events to all links where hostname is not equal to current (native) one.

Solution was easy but wasn’t work to FF.

Some googling + small friends help and:

function extLinks(){
  if (!document.getElementsByTagName) return false;
   var objLinksArr=document.body.getElementsByTagName('A');
   var currentDomain=top.location.hostname;
   var seqURL='';
   for (eachLink in objLinksArr){
     if(objLinksArr[eachLink].title){
        customText=objLinksArr[eachLink].title;
      }
     if(objLinksArr[eachLink].href){
       seqURL=objLinksArr[eachLink].href;
       seqURL=seqURL.replace(/http:\/\//gi,'');	
	 seqURL=seqURL.replace(/https:\/\//gi,'');
	 seqURL=seqURL.split('/');
	 if(seqURL[0]!=currentDomain){
		 addEvent(objLinksArr[eachLink], 'click', cancel);
         }
       }
   }
}

function addEvent(obj, evType, fn){  
	if (obj.addEventListener) {
	    obj.addEventListener(evType, fn, false);
    	return true;
	}else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn);
	    return r;
    }else{
    return false;
  }
}

function cancel(e) {
 if(confirm('Are you sure?')){
 	if(e.srcElement)
 	e.srcElement.target='_blank';
	else
	e.target.target='_blank';
 }else{
  if (e && e.preventDefault)
    e.preventDefault(); // DOM style
  return false; // IE style
  }
}

window.onload=extLinks;

Big thanks to Gerd Riesselmann and his article: Firefox canceling problem solved

 

Comments are closed.