Firefox getElementById problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wangers16
    New Member
    • Jul 2007
    • 57

    Firefox getElementById problem

    Hi all,

    I have a checkbox on my site that needs it's status to be changed dynamically depending on the status of a cookie, the script I have works fine in IE yet not in Firefox, I have tried getElementById, getElementsByNa me and placing the checkbox in a form and obtaining the element that way but nothing seems to work, Firefox can't seem to find the checkbox.

    Here is the code that I am currently using

    Code:
    function infocheck(){
    var disabled = getCookie('disabled');
    var box = document.settings.toggle;
    var pageid = getCookie('currpage');
    if (pageid == pid){
    	if (disabled === null){
    		box.checked = true;
    	}
    	else{
    		box.checked = false;
    	}
    }
    }
    any suggestions?

    Thanks in advance.
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    Hi Dude, it seems there is nothing problem with the code, just check the mozilla browser property wether the cokkie is enabled or not. Because there are two methods for maintaining a session 1) through Cookies 2) URL rewritting. In most of the browser defaultly cookies will be set disabled. Just check that once.

    Regards
    Ramanan Kalirajan

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Post the HTML code for the checkbox. Is it just a single checkbox and is the ID unique? Have you checked the error console for errors?

      Comment

      • wangers16
        New Member
        • Jul 2007
        • 57

        #4
        I have cookies enabled and here is the code for the checkbox

        Code:
        <label for="toggle">Info Bar on/off</label> <input type="checkbox" id="toggle" name="toggle" onclick="barstat()" value="" disabled>
        Yes it is just a single checkbox with a unique ID

        I have also checked the error console and this is what it is displaying "box is null"

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Is the name of the form settings?

          "box is null" means that it's not getting the checkbox element. Try using the ID, i.e. box = document.getEle mentById("toggl e").

          Comment

          • wangers16
            New Member
            • Jul 2007
            • 57

            #6
            yes the name of the form is settings and I have tried that but it still comes up with the same error

            I have also tried the box outside of a form as well, but to no aveil

            it may or may not be worth mentioning that this error seems to pop up on both Firefox 2 and 3

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              How and when are you calling the infocheck() function?

              Can you post the rest of the relevant code or a version that demonstrates the problem or a link?

              Comment

              • wangers16
                New Member
                • Jul 2007
                • 57

                #8
                The function is being called onload along with some other functions, however the snag is that there is another part of the function which does something else but that works perfectly in both

                here is the full code at the moment

                Code:
                function infocheck(){
                var disabled = getCookie('disabled');
                var box = document.getElementById('toggle');
                var pageid = getCookie("currpage");
                if (pageid == pid){
                	if (disabled === null){
                		box.checked = true;
                	}
                	else{
                		box.checked = false;
                	}
                }
                else{
                	/*THIS PART ONWARDS IS WORKING PERFECTLY (somehow)*/if (pageid=="pps"){
                		if (getCookie("mpwstat")=="yes"){
                			history.back(1);
                		}
                		else{
                			return false;
                		}
                	}
                	else{
                		return false;
                	}
                }
                }
                and this is how it is being called, I had to use a custom onload function because of other scripts

                Code:
                load(setTimeout('infocheck()',1));
                I think that is all that may be needed let me know if you need any more

                P.S. Cannot provide a link as it is not hosted yet

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Can you post the getCookie function code too. What's the value of pid?

                  Comment

                  • wangers16
                    New Member
                    • Jul 2007
                    • 57

                    #10
                    the value of pid is pref

                    getCookie function:
                    Code:
                    function getCookie(cookie_name){
                      var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
                    
                      if (results){
                        return (unescape(results[2]));
                    }
                      else{
                        return null;
                    }
                    }

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Hm, ok, the best thing would be for you to post the whole code for the page leaving out the irrelevant parts (test it to see if the problem still occurs). If it's too much code, post it as an attachment.

                      Comment

                      • wangers16
                        New Member
                        • Jul 2007
                        • 57

                        #12
                        well as far as I can see, all that is relevant to the problem is the coding that was posted earlier, other than the content in the head tags and the declaration the rest is completly irrelevant to the problem.

                        I will post the declaration and the head tags anyway.

                        Code:
                        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                        <html>
                        
                        <head>
                        <title>Settings</title>
                        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                        <script type="text/javascript" src="js/mpw.js"></script>
                        </head>
                        error is still occuring

                        let me know if you need any more :)

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          What's in the mpw.js JavaScript file? Are the cookie functions in there? Where is pid set? I think if you could produce a mini-version of the page with all irrelevant code stripped out that still produces the error, that would help pinpoint the error. Add a few alerts in too to check that the values of variables are what you expect (or you could use a debugger such as Firebug).

                          Comment

                          • wangers16
                            New Member
                            • Jul 2007
                            • 57

                            #14
                            I have just created a mini version, and it seems to work perfectly, however when I link it up to the main site script (mpw.js) it seems to stop working and starts producing the error again.

                            I think it may be something to do with the other script that loads onload.

                            The other script that is being called is the "Obtain HREF's" one that you helped me with before, and when I cancel that out it seems to work ok.

                            I think it could either be a conflict between this script and the valbeta() script or this script and the custom onload script used.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Now we seem to be getting somewhere! Can you show the code for the custom onload script?

                              Comment

                              Working...