473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Variable Scoping in include files

I seem to be have problems with what appears to be variable scoping. If
I define a constant in my main HTML page, I do not seem to be able to
reference it within functions in JavaScript include files.

For instance, I receive the following errors:

Error: CONST0 is not defined
Source File: test1.js
Line: 3

Error: CONST0 is not defined
Source File: test2.js
Line: 4

Error: document.getElementById("div1") has no properties
Source File: jstest1.html
Line: 18

=========== BEGIN jstest1.html ======================
<html>
<head>
<script type="text/Javascript" src="test1.js"></script>
<script type="text/Javascript" src="test2.js"></script>

<script type="text/javascript" language="JavaScript">
<!--
const CONST0 = "foo";

document.getElementById("div1").innerHTML = 'This is from
test1.html:<br>CONST0 = ' + CONST0 + "<br>" +
'CONST1 = ' + CONST1 + "<br>" +
'CONST2 = ' + CONST2 + "<br>" ;
//-->
</script>

</head>
<body bgcolor="white">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

</body>
</html>
=========== END jstest1.html ======================

=========== BEGIN test1.js ======================

const CONST1 = "This is from test1";

document.getElementById("div1").innerHTML = "This is from
test2.html:<br>CONST0 = " + CONST0 + "<br>" +
"CONST1 = " + CONST1 + "<br>" +
"CONST2 = " + CONST2 + "<br>" ;

=========== END test1.js ======================

=========== BEGIN test2.js ======================
const CONST2 = "This is from test2";

document.getElementById("div1").innerHTML = "This is from
test1.js:<br>CONST0 = " + CONST0 + "<br>" +
"CONST1 = " + CONST1 + "<br>" +
"CONST2 = " + CONST2 + "<br>" ;
=========== END test2.js ======================
Aug 24 '05 #1
3 4009
John Yopp wrote:
I seem to be have problems with what appears to be variable
scoping. If I define a constant in my main HTML page,
I do not seem to be able to reference it within functions
in JavaScript include files.
The code that appears to be referencing this 'constant' does not appear
to take the form of functions. If the included files attempt to
reference the 'constant' in inline code then that code should not be
executed until after the 'constant' has been defined. The order in which
SCRIPT elements appear in a page is the order in which their global
declarations and inline code is evaluated (unless the DEFER attribute is
used, and implemented in the browser environment).

<snip> <script type="text/Javascript" src="test1.js"></script>
<script type="text/Javascript" src="test2.js"></script>
So move the following SCRIPT element above the previous two.
<script type="text/javascript" language="JavaScript">
The language attribute is redundant in the company of the type
attribute.
<!--
This 'hide scripts from older browsers' HTML-style comment is redundant
as those 'older browsers' are now long-since dead and gone.
const CONST0 = "foo";

<snip>

'const' is a reserved word in ECMA 262, it is a JavaScript(tm)
extension, introduced at about version 1.4 (as I recall). It will be a
syntax error in ECMA 262 implementations that do not follow
JavaScript(tm) 1.4 extensions (which is most of them).

Richard.
Aug 25 '05 #2

John Yopp wrote:
I seem to be have problems with what appears to be variable scoping. If
I define a constant in my main HTML page, I do not seem to be able to
reference it within functions in JavaScript include files.
[snip]
<script type="text/javascript" language="JavaScript">
<!--
const CONST0 = "foo";
There is no "const" keyword in javascript, although it is a reserved
word. So, just say:

var CONSTO = "foo";

Be sure to remember not to change the value.
document.getElementById("div1").innerHTML = 'This is from
test1.html:<br>CONST0 = ' + CONST0 + "<br>" +
'CONST1 = ' + CONST1 + "<br>" +
'CONST2 = ' + CONST2 + "<br>" ;
//-->
</script>
[snip] const CONST1 = "This is from test1"; [snip] const CONST2 = "This is from test2";


Same applies to both above.

Hope this helps. :)

Aug 25 '05 #3
ASM
John Yopp wrote:
I seem to be have problems with what appears to be variable scoping. If
I define a constant in my main HTML page, I do not seem to be able to
reference it within functions in JavaScript include files.

For instance, I receive the following errors:

Error: CONST0 is not defined
yes CONSTO is not a variable
Source File: test1.js
Line: 3

Error: CONST0 is not defined
same error
Source File: test2.js
Line: 4

Error: document.getElementById("div1") has no properties
see bellow
Source File: jstest1.html
Line: 18

=========== BEGIN jstest1.html ======================
<html>
<head>
<script type="text/Javascript" src="test1.js"></script>
<script type="text/Javascript" src="test2.js"></script>

<script type="text/javascript" language="JavaScript">
<!--
const CONST0 = "foo";
var CONSTO = 'foo';
document.getElementById("div1").innerHTML = 'This is from
test1.html:<br>CONST0 = ' + CONST0 + "<br>" +
'CONST1 = ' + CONST1 + "<br>" +
'CONST2 = ' + CONST2 + "<br>" ;
You can't call at this time this div 'div1'
that appears only bellow

Try putting all this script in end of page
of set a function to do this job
this function will be called when page is loaded :

function init() {
document.getElementById("div1").innerHTML = 'This'+
' is from test1.html:<br>CONST0 = ' + CONST0 + "<br>" +
'CONST1 = ' + CONST1 + "<br>" +
'CONST2 = ' + CONST2 + "<br>" ;
}
onload = init;
//-->
</script>

</head>
<body bgcolor="white">
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>

</body>
</html>
=========== END jstest1.html ======================

=========== BEGIN test1.js ======================

const CONST1 = "This is from test1";
var CONST1 = "This is from test1";
not the moment to call this div (same error as above)
document.getElementById("div1").innerHTML = "This is from
test2.html:<br>CONST0 = " + CONST0 + "<br>" +
"CONST1 = " + CONST1 + "<br>" +
"CONST2 = " + CONST2 + "<br>" ;

=========== END test1.js ======================

=========== BEGIN test2.js ======================
redito
const CONST2 = "This is from test2";

document.getElementById("div1").innerHTML = "This is from
test1.js:<br>CONST0 = " + CONST0 + "<br>" +
"CONST1 = " + CONST1 + "<br>" +
"CONST2 = " + CONST2 + "<br>" ;
=========== END test2.js ======================

--
Stephane Moriaux et son [moins] vieux Mac
Aug 25 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
7
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
17
by: ethan | last post by:
Hi All, How to write a macro with variable length of argument? For example, if i need a macro to check return value of printf. #define PRINTF_ESC(x, ...) if(printf(x, ...) == 10) goto end; ...
5
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
2
by: hrishikesh.mehendale | last post by:
Hi All, Sorry if I'm just repeating other questions, but I can't find an answer for this particular problem: I have my main index.php file, which looks so: <?php include "includes/header.php";...
9
by: NevilleDNZ | last post by:
Can anyone explain why "begin B: 123" prints, but 456 doesn't? $ /usr/bin/python2.3 x1x2.py begin A: Pre B: 123 456 begin B: 123 Traceback (most recent call last): File "x1x2.py", line 13,...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
11
by: sfncook | last post by:
I have some code that needs to declare an STL Map in the static scope, insert values into the map in the function of one class and access the values in the functions of other classes. This seems...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.