Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add onclick to all links on page. 1

Status
Not open for further replies.

bdichiara

Programmer
Oct 11, 2006
206
US
I'm trying to find an easy way to add a confirm dialog to all links that take the user away from our website without doing it manually for each link. The issue is that none or very few of the links have IDs. So how do i add onclick to links that don't have an id? Here's my code:
Code:
function fixExitLinks(){
  var myLinks = document.links;
  for(var i=0;i < myLinks.length;i++) {
    var thehref = myLinks[i].href.toString();
    if(thehref.indexOf("mydomain.com") == -1){
      myLinks[i].onclick = return confirmExit();
    }
  }
}

function confirmExit(){
  if(confirm('You are leaving our site')){
    return true;
  } else {
  	return false;
  }
}
Code:
<body onLoad="fixExitLinks();">

_______________
_brian.
 
Ok, I've got this working for Firefox, but not for IE. Any ideas?
Code:
function fixExitLinks(){
  var myLinks = document.links;
  for(var i=0;i < myLinks.length;i++) {
    var thehref = myLinks[i].href.toString();
    if(thehref.indexOf("alatelco.org") == -1){
       myLinks[i].setAttribute('onclick','return confirmExit();');
       myLinks[i].setAttribute('target','_blank');
    }
  }
}

_______________
_brian.
 
If confirmExit is not to be changed, I would not change your moz/ff part using setAttribute() method though conceptually unsatisfactory. I expand it to include the support for ie.
[tt]
function fixExitLinks(){
var myLinks = document.links;
var btype=0;
if (window.attachEvent) {
b=1;
} else if (window.addEventListener) {
b=2;
}
for(var i=0;i < myLinks.length;i++) {
var thehref = myLinks.href.toString();
if(thehref.indexOf("mydomain.com") == -1){
switch (b) {
case 1:
myLinks.attachEvent("onclick",confirmExit);
break;
case 2:
myLinks.setAttribute("onclick","return confirmExit()")
break;
default:
//do nothing
}
}
}
}
[/tt]
For moz/ff, in many situations, it is more appropriate to use addEventListener method to do the job. But, unless your confirmExit is scripted with the reference to the event object, use setAttribute method would be simpler.
 
Excellent work! that helped a lot! Thanks!

_______________
_brian.
 
Ok, that worked great on all links with a href="", however I have one more obstacle that's not so important, but will be really awesome if we can figure out a way to get it to work.

the site has a DHTML menu on it. Built with something called Xtreeme DHTML Menu Studio. It attaches a JS file to the page which holds all the links and menu hierarchy. That file then attaches another file at the end that builds the menu's and adds the onClick to all of the links to make them work. Is there a way to fix these links in the same way?

Sample:
_______________
_brian.
 
tsuji's code should do it, provided, of course, it is run after the menu has been built...

this also supposes that the menu is built using <a></a> for the actions and not an onClick event on a span or other tag. If so you could adapt tsuji's code to cater for the alternative without hassle.
 
Well, I have the function set to "onLoad". Is it possible the menu is getting built after that event? I'll check on what tags are being used. they may be something other than <a> tags.

_______________
_brian.
 
Ok, i have ensured that this event occurs after the menu is built, however I don't think the menu is using <a> I believe it's using onClick events. So, I guess I would need to do this twice and check all "onclick"s.

Any idea on how to go about doing this? Is there a way to find out the onclick value of a link that doesn't have an ID?

_______________
_brian.
 
Let me get this straight.
Before I can leave your website, I have to confirm that I (really) want to leave?

Guess how many times I'd ever come back to your site? Find Answer here:
Code:

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
It's for legal purposes to let the visitor know they are leaving the site and that information found on outside webpages does not express the opinion of our company...

I'm just doing what i'm told to do...

_______________
_brian.
 
ok.
A simple note should cover that for legal purposes, but I certainly understand corporate lawyers. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top