Forum Moderators: open

Message Too Old, No Replies

document.links & lastIndexOf

unexpected stuff

         

nyteshade

5:34 pm on Sep 12, 2011 (gmt 0)

10+ Year Member



Two questions on the function below that executes at document load time.

1: document.links at LINE1 below contains just the file name and not the href absolute URL? Since the function fails on document.link[0] I can only say it contains: index.php, when it should contain [localhost...] right?

2: mystring.lastIndexOf (LINE2) fails with 'lastIndexOf not a function' when it certainly is and I am using it several times in another page with almost the exact syntax.

function links_onclick(){
for(var i=0; i<document.links.length; i++){
var mystring = document.links[i];//LINE1
var myfilename = mystring.substr(mystring.lastIndexOf("/") + 1);//LINE2
if(myfilename == "phantom.html"){
window.document.links[x].onclick = function(){
var msg = "";
msg += "Invoked by onclick property of the a element.\n\n";
msg += "Invoked function will cancel the href action by\n";
msg += "returning 'false'.\n";
alert(msg);
return false;
}
}
}
}


I'm learning about creating behaviors as properties and this looks right and I've used document.links and lastIndexOf tens of times but I may have my load and rendering concept twisted? Any help much appreciated. Thanks all.

penders

7:04 pm on Sep 12, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



var mystring = document.links[ i ];


document.links[ i ] is an HTMLAnchorElement, not a string. So you will need document.links[ i ].href to get the href property.

And that should solve your lastIndexOf problem as well.

nyteshade

7:57 pm on Sep 12, 2011 (gmt 0)

10+ Year Member



Pesky objects, thanks penders.