...

View Full Version : on readyState 1,2,3 disable all links on page



tempest4000
01-06-2009, 12:28 PM
Hi,

I need to disable all links on page when Ajax readyState is 1, 2 or 3 and then enable them when ready state is 4.

I need a function that disables and enables links. Ive got the below function that disables links but doesn't re-enable them. Im not too familiar with javascript and would appreciate any help to get this function working properly.

The Code


function returnTrue() { return true; }
function returnFalse() { return false }
function EnableLinks(enable){

objLinks = document.getElementById("reports").getElementsByTagName("a");

if(enable) {
for(i=0;i<objLinks.length;i++){
var link = objLinks[i];
link.onclick=returnTrue;
link.style.textDecoration="underline";
link.style.cursor="pointer";
}
} else {
for(i=0;i<objLinks.length;i++){
var link = objLinks[i];
link.onclick=returnFalse;
link.style.textDecoration="none";
link.style.cursor="default";
}
}
}


many thanks in advance for your help

itsallkizza
01-06-2009, 05:16 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Example</title>
<style type="text/css">
</style>
<script type="text/javascript">
// <![CDATA[

var enable_links = true;
function setup_anchors()
{
var as = document.getElementsByTagName("a");
for (var i=0;i<as.length;i++)
{
as[i].do_this_first = as[i].onclick || new Function();
as[i].onclick = function()
{
this.do_this_first();
return enable_links;
}
}
}

function disable_anchors()
{
enable_links = false;
}

function enable_anchors()
{
enable_links = true;
}

function doSomething()
{
var something = "done";
alert(something);
}

window.onload = function()
{
setup_anchors();
}

// ]]>
</script>
</head>
<body>

<a href="http://www.google.com" onclick="doSomething();">google</a><br />
<a href="http://www.yahoo.com">yahoo</a><br />

<br />
<input type="button" onclick="disable_anchors()" value="disable anchors" />
<input type="button" onclick="enable_anchors()" value="enable anchors" />

</body>
</html>

tempest4000
01-07-2009, 10:43 AM
Hi,
Thanks for your help, but I get an error "Too much recurrsion" on the line: this.do_this_first();

I have commented this line out and the function works when disabling links but does not re-enable all of them. It re-enables links such as < a href=""></a> but not the links that have
< a href="" Onclick=""></a>. I think that the Onclick values that I had have been overwritten with onclick true and therefore do not activate the functions.

itsallkizza
01-07-2009, 03:38 PM
I think that the Onclick values that I had have been overwritten with onclick true and therefore do not activate the functions.- That's what the anchor.do_this_first function accounts for. If the anchor already has an onclick, then it copies it, hooks it up to a reference, then calls it before returning false. You'll notice in the example I gave, the doSomething function is still called, regardless of whether the links are disabled or not.

Post your html code so I can take a look. It's possible (though unlikely) you just have so many links on your page that this code is freezing up. More likely is my code is conflicting with another function of yours (although I can't really imagine how right now).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum