Web Analytics Made Easy -
StatCounter Can you access global variables in a .htm from .js? - CodingForum

Announcement

Collapse
No announcement yet.

Can you access global variables in a .htm from .js?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can you access global variables in a .htm from .js?

    Hey guys,

    quick question. I have a html document portion with the following lines in it

    Code:
    <script LANGUAGE="JavaScript" type="text/javascript" src="cookie.js"></script>
    
    <script LANGUAGE="JavaScript" type="text/javascript">
    
    function bodyload()
    {
    	if (loggedon != true) { checklogin(); }
    	
    	if (loggedon == true) { reloadimg(); }
    	else { hasloggedon();}	
    }
    </script>
    </head>
    
    <body onload="bodyload()">
    and a Javascript Document "cookie.js" file

    Code:
    var loggedon = false;
    
    function checklogin()
    {
    	if (loggedon == true) return true;
    	else
    	{
    		if (cookie_set != true ) testSessionCookie();
    		{
    			if ( readCookie() == true )  //already logged in
    			{
    				loggedon = true;
    				return true; //let them navigate to that page
    			}
    			else
    			{
    				loggedon = false;
    				return false; //they must goto login page
    			}
    		}
    	}		
    }
    For some reason, the .htm file will not recognise the "loggedon" variable created in the "cookie.js" file. My background is in C, and i am HOPING that by including the ".js" file in the .htm document, it acts as a type of "header file" incorporating the global variables, and global functions of this file. I cant work out why it believes the variable is undefined, unless this is not the case.

    Is there a way to do this without resorting to dumping all of my javascript code into the "htm" file?

    Thanks In Advance Again
    Cheers
    Adam

  • #2
    Is the path to the js correct? Put an alert in the js file to see if it's being loaded.
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      Hey Glenn,

      thanks for the reply.

      Yes, the path is correct. I know this because there are other functions in the same file that i am running from another page, they work fine. The whole thing is running on an embedded system, so there is no directory structure also, only the files stored in flash.

      Can you see anything wrong with what ive done? Can you even reference variables from another file? Im very new to javascript, but apart from a few discrepancies like this its quite similar to C, which is nice.

      I look forward to your reply
      Thanks again
      Adam

      Comment


      • #4
        I see nothing wrong in the code, unless I'm missing something. Yes you can access the global variable in the external js from inline scripts. It's as if the included js is written directly in the page. If you put alert loggedon inside the bodyLoad function, does it say undefined?
        Code:
        function bodyload()
        {
           alert(loggedon);
           ...
        Glenn
        vBulletin Mods That Rock!

        Comment


        • #5
          Hey Glenn,

          i managed to get rid of the error (somehow), however...now...in Mozilla (using the javascript development tools) it says that the FUNCTION bodyload() is undefined. I cant see how this can be! It is strictly included at the top of my htm file. One thing that ive noticed, is that in Mozilla, it brings up a set of javascript errors about some functions not being defined, however in IE6 the javascript runs but there are a number of "object expected" errors. If i run it on IE6 a number of times, the "object expected errors" appear on different lines, it appears randomly! Doesnt make sense. Ive done some research on this, and discovered that this can happen when either braces "{,}" or brackets "(,)" are either left open, or are too many closing brackets, braces...ive checked this, and this is NOT the case also.

          This will really burst your bubble, as a last resort, i opened my page up in Frontpage and went to the "Preview" pane, and what do you know...it works perfectly! This...i cannot explain. Does anyone have any ideas or suggestions?

          Cheers

          Adam

          Comment


          • #6
            Do you have the page online?
            Glenn
            vBulletin Mods That Rock!

            Comment


            • #7
              Nope, its an embedded web interface for a particular device that hasnt yet been released. Im only a trainee here, so im not sure about the confidentiality of releasing it "into the wild" if you know what i mean and putting it online.

              Have you ever come across a problem like this before?

              Adam

              Comment


              • #8
                But are you testing it with PC-based browsers?

                Object expected error means that the browser cannot find the function being called, bodyload() in this case. Check the spelling and case and also if you spelled the script tag and its attributes correctly.
                Glenn
                vBulletin Mods That Rock!

                Comment


                • #9
                  Yep testing with PC based browsers. The embedded system has a webserver system on it, which works well.

                  The case and all attributes appear to be correct. It works properly under frontpage, im just at a loss trying to figure out why it wont work under normal browsers.

                  Comment


                  • #10
                    What did you do before the Object Expected error appeared? How did you get rid of the loggedon variable problem?
                    Glenn
                    vBulletin Mods That Rock!

                    Comment


                    • #11
                      From the HTML file i was doing a

                      Code:
                      <body onload="if(loggedon==true) {bodyload();} else {someotherfunction();}">
                      *some code here*
                      </body>
                      i am now doing a

                      Code:
                      <body onload="bodyload()">
                      *some code here*
                      </body>
                      so essentially it is the same error, it cannot find the variable/function in the .js file that is added. Its just got me buggared why it works under frontpage!

                      Comment


                      • #12
                        Don't trust Frontpage.

                        If you do this in the js file, is the alert displayed when the page is loaded?
                        Code:
                        [B]alert("this alert is from js!");[/B]
                        
                        var loggedon = false;
                        
                        function checklogin(){
                         ...
                        }
                        Glenn
                        vBulletin Mods That Rock!

                        Comment


                        • #13
                          Nope. Its not.

                          Comment


                          • #14
                            So that means the js file not properly loaded.
                            Do you have HTML comments <!-- --> inside the js file?
                            You should remove it.
                            Glenn
                            vBulletin Mods That Rock!

                            Comment


                            • #15
                              Yep, done that....still no difference

                              there is a page that is VERY similar to this one, that works fine! therefore, im not so sure now that the problem lies in the javascript, rather in the HTML.

                              The only problem now is that, the only DIFFERENCE between these 2 pages is the fact that i on the one that DOESNT work, i use a .jpg image as a background picture.

                              You see the webpage/s that im trying to build have a dynamic image that changes via a c program running on the embedded device with the webserver, this is only the interface. The image is actually "a copy" of the LCD screen displayed on the device. The page that works was designed for PDA screens, and ONLY displays a 320x240 dynamic image, with "hot spots" for navigating that are laid over the top of the image.

                              The other page (the one that DOESNT work) has a background .jpg of the actual full controller, with the buttons displayed and the LCD screen in the middle of the control (like on the real device). I have used hotspots where the buttons are also. This is the only difference between the 2 pages, only one works and one doesnt.

                              The header is EXACTLY the same, leading me to think that it is something else within the code to do with the .jpg or the hotspots.

                              I use a MAP to describe the hotspots on both pages, however on the PDA i use "href" commands with the argument of the button coming after the link. ie. if the button "L1" was pressed, the link would show "http://X.X.X.X/elt.htm?L1". Whereas with the other page, i call a function that reloads the dynamic .bmp file, without needing to reload the background .jpg file again...if this makes sense? The problem is that it CANT FIND this reload function, because it is placed in the .js file.

                              I hope ive made some sense?

                              Thanks a lot again for your time,

                              if you or anyone else can suggest anything?

                              Adam

                              Comment

                              Working...
                              X