Forum Moderators: open

Message Too Old, No Replies

Disable Links Script Works, Just Not On iFrames.

endless loop?

         

shadowpwner

3:47 am on Jul 25, 2009 (gmt 0)

10+ Year Member



I recently wrote a script that disables all the links on a page by using a loop to add onclick return false. However, when I try to add an iFrame to the code, the script no longer works. Here's the code:

<html>
<head>
<script>
function DisableEnableLinks(xHow){
objLinks = document.links;
for(i=0;i<objLinks.length;i++){
objLinks[i].disabled = xHow;
//link with onclick
if(objLinks[i].onclick && xHow){
objLinks[i].onclick = new Function("return false;" + objLinks[i].onclick.toString().getFuncBody());
}
//link without onclick
else if(xHow){
objLinks[i].onclick = function(){return false;}
}
//remove return false with link without onclick
else if(!xHow && objLinks[i].onclick.toString().indexOf("function(){return false;}") != -1){
objLinks[i].onclick = null;
}
//remove return false link with onclick
else if(!xHow && objLinks[i].onclick.toString().indexOf("return false;") != -1){
strClick = objLinks[i].onclick.toString().getFuncBody().replace("return false;","")
objLinks[i].onclick = new Function(strClick);
}
}
}

String.prototype.getFuncBody = function(){
var str=this.toString();
str=str.replace(/[^{]+{/,"");
str=str.substring(0,str.length-1);
str = str.replace(/\n/gi,"");
if(!str.match(/\(.*\)/gi))str += ")";
return str;
}
</script>
</head>
<BODY bgcolor="#FFFFFF" onload="DisableEnableLinks(true)">
<a href="http://www.example.com"> This Link Is Disabled </a>

<iframe src="http://example.com"></iframe>

</body>
</html>

Thanks in advance for your help. I am truly perplexed at this problem.

whoisgregg

1:43 pm on Jul 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, shadowpwner!

I would try alerting the object handle in the loop to see if the iframe is being grabbed in that function.

for(i=0;i<objLinks.length;i++){ 
alert(objLinks[i]);

If the iframe is being picked up in

document.links
then just wrap the entire loop with a check that the object handle references an <a> tag as expected.

Added: Is the problem that the script doesn't work on the links inside the iframe? If so, make sure the iframe points to the same domain. Javascript can't interact with pages from a different domain.