By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

Firefox getElementById problem

P: 57
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, getElementsByName 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

Expand|Select|Wrap|Line Numbers
  1. function infocheck(){
  2. var disabled = getCookie('disabled');
  3. var box = document.settings.toggle;
  4. var pageid = getCookie('currpage');
  5. if (pageid == pid){
  6.     if (disabled === null){
  7.         box.checked = true;
  8.     }
  9.     else{
  10.         box.checked = false;
  11.     }
  12. }
  13. }
  14.  
any suggestions?

Thanks in advance.
Aug 7 '08 #1
Share this Question
Share on Google+
20 Replies


RamananKalirajan
100+
P: 607
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
Aug 7 '08 #2

acoder
Expert Mod 15k+
P: 16,027
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?
Aug 7 '08 #3

P: 57
I have cookies enabled and here is the code for the checkbox

Expand|Select|Wrap|Line Numbers
  1. <label for="toggle">Info Bar on/off</label> <input type="checkbox" id="toggle" name="toggle" onclick="barstat()" value="" disabled>
  2.  
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"
Aug 10 '08 #4

acoder
Expert Mod 15k+
P: 16,027
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.getElementById("toggle").
Aug 10 '08 #5

P: 57
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
Aug 10 '08 #6

acoder
Expert Mod 15k+
P: 16,027
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?
Aug 10 '08 #7

P: 57
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

Expand|Select|Wrap|Line Numbers
  1. function infocheck(){
  2. var disabled = getCookie('disabled');
  3. var box = document.getElementById('toggle');
  4. var pageid = getCookie("currpage");
  5. if (pageid == pid){
  6.     if (disabled === null){
  7.         box.checked = true;
  8.     }
  9.     else{
  10.         box.checked = false;
  11.     }
  12. }
  13. else{
  14.     /*THIS PART ONWARDS IS WORKING PERFECTLY (somehow)*/if (pageid=="pps"){
  15.         if (getCookie("mpwstat")=="yes"){
  16.             history.back(1);
  17.         }
  18.         else{
  19.             return false;
  20.         }
  21.     }
  22.     else{
  23.         return false;
  24.     }
  25. }
  26. }
  27.  
and this is how it is being called, I had to use a custom onload function because of other scripts

Expand|Select|Wrap|Line Numbers
  1. load(setTimeout('infocheck()',1));
  2.  
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
Aug 10 '08 #8

acoder
Expert Mod 15k+
P: 16,027
Can you post the getCookie function code too. What's the value of pid?
Aug 10 '08 #9

P: 57
the value of pid is pref

getCookie function:
Expand|Select|Wrap|Line Numbers
  1. function getCookie(cookie_name){
  2.   var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
  3.  
  4.   if (results){
  5.     return (unescape(results[2]));
  6. }
  7.   else{
  8.     return null;
  9. }
  10. }
  11.  
Aug 10 '08 #10

acoder
Expert Mod 15k+
P: 16,027
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.
Aug 11 '08 #11

P: 57
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.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3.  
  4. <head>
  5. <title>Settings</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <script type="text/javascript" src="js/mpw.js"></script>
  8. </head>
  9.  
error is still occuring

let me know if you need any more :)
Aug 13 '08 #12

acoder
Expert Mod 15k+
P: 16,027
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).
Aug 13 '08 #13

P: 57
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.
Aug 13 '08 #14

acoder
Expert Mod 15k+
P: 16,027
Now we seem to be getting somewhere! Can you show the code for the custom onload script?
Aug 13 '08 #15

P: 57
Here is the custom onload function:

Expand|Select|Wrap|Line Numbers
  1. function load(func) {   
  2.   var oldonload = window.onload;   
  3.   if (typeof window.onload != 'function') {   
  4.     window.onload = func;   
  5.   }
  6.   else{   
  7.     window.onload = function() {   
  8.       if (oldonload) {   
  9.        oldonload();   
  10.       }
  11.       else{   
  12.       func;
  13.       }   
  14.     }
  15.   }   
  16. }
  17.  
Aug 13 '08 #16

acoder
Expert Mod 15k+
P: 16,027
You can avoid using this custom script by using addEventListener/attachEvent. See this link for more information. If that fails to solve the problem, can you remind me of the valbeta script.
Aug 14 '08 #17

P: 57
Sorry for the delayed response, first time in a while I have been able to check back here

I have looked through the page that you provided and i'm not entirely sure where to put the addEventListener(), in relation to the page or script?

Here is the code for the valbeta script
Expand|Select|Wrap|Line Numbers
  1. function valbeta() {
  2.   var anchors = document.getElementsByTagName('a');
  3.   for (var o = 0; o < anchors.length; ++o){
  4.     anchors[o].onclick=function(){
  5.       validate(this.getAttribute('href',2));
  6.       return false;
  7.     };
  8.   }
  9. }
  10.  
Sep 12 '08 #18

acoder
Expert Mod 15k+
P: 16,027
It's been so long that I forgot what this was about.

To use addEventListener/attachEvent is easy. Wherever you would call your custom onload function, add an event instead.
Sep 12 '08 #19

P: 57
Once again, sorry for taking so long to reply, but thank you for all of your help, it is working perfectly now. :)
Oct 3 '08 #20

acoder
Expert Mod 15k+
P: 16,027
No problem. At least it's working and it's better late than never ;)
Oct 4 '08 #21

Post your reply

Sign in to post your reply or Sign up for a free account.