Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    on readyState 1,2,3 disable all links on page

    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

    PHP 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
    Last edited by tempest4000; 01-06-2009 at 02:56 PM.

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    Code:
    <!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>
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by tempest4000; 01-07-2009 at 12:03 PM.

  • #4
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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).
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •