tags:

views:

85

answers:

5

I tried doing this but it didn't seem to work:

 window.onload = initAll;


    function initAll(){
    document.getElementsByTagName('a').onclick = clickHandler;
     }

    function clickHandler(){
        if(this.toString().indexOf("localhost") < 0) {
            confirmation = confirm("You are now leaving http://soso.com.  Please click 'ok' to continue to this site, or 'cancel' to stay in http://soso.com");
                if (confirmation == false){
                    return false;
                }

        }

    }

I know I can getElementById and that works, but it doesnt work this way. Any help would be appreciated.

Thanks!

+1  A: 

You need to iterate over all of the elements returned by document.getElementsByTagName

var links = document.getElementsByTagName('a');
var i = links.length;
while ( i-- ) {
    links[i].onclick = clickHandler;
}
// ... rest of your code ...
Sean Vieira
your condition is wrong. should be `i > 0`. also you can combine your decrement and condition to get `while ( i-- )`.
lincolnk
What about `while( i-- ) { links[i].onclick = clickHandler; }`?
palswim
@lincolnk @palswim -- good points ... updated to be slightly more sensible (and work ... you know, while we're fixing things) :-) Thanks!
Sean Vieira
+1  A: 
function initAll() 
{
    var elements = document.getElementsByTagName('a'); // returns an array

    // add the handler to each element
    var n;
    for (n = 0; n < elements.length; ++n)
        elements[n].onclick = clickHandler;
}
egrunin
Replace with `int` with `var`, that is not valid Javascript.
CD Sanchez
int n; :O :O . There is no int in JavaScript.
Rajat
Also, you might wanna compute that length property once. It might cause serious issues in this case as getElementsByTagName returns a live list of elements.
Rajat
@Rajat the list is only going to change if OP specifically changes it, in which case he probably wants an up-to-date length reference. however your suggestion is a good one for optimizing a fixed-length list.
lincolnk
fixed int -> var :)
egrunin
+1  A: 

That's because getElementsByTagName returns a NodeList. You cannot assign an event handler via the onclick property on a NodeList, only a single DOMElement.

Try:

 var elems = document.getElementsByTagName('a');
 for (var i = 0; i < elems.length; i++) {
    elems[i].onclick = clickHandler;
 }
CD Sanchez
A: 

I'd strongly suggest using JQuery. Something like this should do the trick:

$(function(){$('a').click(clickHandler);});
StriplingWarrior
While jQuery *does* make working with the DOM easier, including 24KB of code (minified and gzipped) on the page when 125 **characters** does the trick is rather overkill. ;-)
Sean Vieira
http://lifeinafolder.posterous.com/jquery-nuff-said
Rajat
@Sean: 125 unicode characters will take 4 KB of code. Including all that code inline on your document (which is probably what he's doing) adds up to 24KB quickly over a number of page requests. If you use Google's hosted copy, you can leverage their fast, distributed servers to serve up JQuery quickly, with a high likelihood that the user will have it cached already anyway. It also means you can use JQuery anywhere else in your application to create concise, cross-browser-compatible code. Unless this is the only place he's going to use javascript, I'd say it's a no-brainer.
StriplingWarrior
@StriplingWarrior: "125 unicode characters will take 4 KB of code." You sure about that? In UTF-8 that would be 125 bytes, no? And in UTF-32, at 125 * 4 or 500 bytes.
CD Sanchez
+6  A: 

document.getElementsByTagName('a') returns a NodeList of DOM Elements. So for starters, you will have to iterate over them and attach a handler to each like this :

var links = document.getElementsByTagName('a');
for( var i=0,il = links.length; i< il; i ++ ){
 links[i].onclick = clickHandler;
}

If there are many elements, I suggest you read about event delegation and assign just one handler for everything.

Rajat
+1 for suggesting event delegation!
Sean Vieira
You forgot a semicolon after the condition.
CD Sanchez
@Daniel Thanks. Corrected!
Rajat