code embedded in html file vs .js file reference

C

comcast news

at least i think it this is the problem
am trying to fix a webpage/javascript that i didn't write
www.deltaneutral.com when invoked or any new page from the menu, get the
error msg
line 2
char 1
syntax error
code 0
there is a java scipt file invoked early on that 'seems' to be the problem,
its just after the body tag
<script language=javascript src=/scripts/swap.js></script>
the code is at the end of this post
however, when i embed the code directly in the page, instead of the file
reference, the error goes away
and the functions seem to execute ok
i have checked the .js file with a hex editor looking for hidden weird bytes
but havent' found any
that look out of the ordinary, so i'm stumped why the seemingly same code
gets the error msg
only when it is invoked via its .js file
also i have loaded the html and .js file on a different, 2nd web server just
to be sure that somehow
the primary server isn't somehow supplying the .js file with a funny byte



<script type="text/javascript">
//alert ("now entering swap2ansi");
function WM_imageSwap(daImage, offOn){
var daPath = "images/";
var daPage = "home";
// Check to make sure that images are supported in the DOM.
if(document.images){
// Check to see whether you are using a name, number, or object
if (typeof(daImage) == 'string') {
// This whole objStr nonesense is here solely to gain compatability
// with ie3 for the mac.

objStr = 'document.' + daImage;
obj = eval(objStr);
srcStr = daPath + "nav_" + daImage + "_" + offOn + ".gif";
//srcEval = eval(daPath);
obj.src = srcStr;

objStr2 = 'document.' + daPage;
obj2 = eval(objStr2);

if (offOn == "on") {
srcStr2 = daPath + "nav_" + daPage + "_off.gif";
obj2.src = srcStr2;
} else {
srcStr2 = daPath + "nav_" + daPage + "_on.gif";
obj2.src = srcStr2;
}

} else if ((typeof(daImage) == 'object') && daImage && daImage.src) {

daImage.src = daPath + "nav_" + daImage + "_" + offOn + ".gif";
if (offOn == "on") {
daPage.src = daPath + "nav_" + daPage + "_off.gif";
} else {
daPage.src = daPath + "nav_" + daPage + "_on.gif";
}
}
}
}


function SimpleSwap(daImage, daSrc){
var objStr,obj;
//alert ("now entering simple swap");
// Check to make sure that images are supported in the DOM.
if(document.images){
// Check to see whether you are using a name, number, or object
if (typeof(daImage) == 'string') {
// This whole objStr nonesense is here solely to gain compatability
// with ie3 for the mac.
objStr = 'document.' + daImage;
obj = eval(objStr);
obj.src = daSrc;
} else if ((typeof(daImage) == 'object') && daImage && daImage.src) {
daImage.src = daSrc;
}
}
}
//
</script>
 
M

Michael Winter

at least i think it this is the problem
am trying to fix a webpage/javascript that i didn't write
www.deltaneutral.com when invoked or any new page from the menu, get
the
error msg
line 2
char 1
syntax error
code 0
there is a java scipt file invoked early on that 'seems' to be the
problem,
its just after the body tag
<script language=javascript src=/scripts/swap.js></script>
the code is at the end of this post
however, when i embed the code directly in the page, instead of the file
reference, the error goes away
and the functions seem to execute ok
i have checked the .js file with a hex editor looking for hidden weird
bytes
but havent' found any
that look out of the ordinary, so i'm stumped why the seemingly same
code
gets the error msg
only when it is invoked via its .js file
also i have loaded the html and .js file on a different, 2nd web server
just
to be sure that somehow
the primary server isn't somehow supplying the .js file with a funny byte



<script type="text/javascript">
//alert ("now entering swap2ansi");
function WM_imageSwap(daImage, offOn){
var daPath = "images/";
var daPage = "home";
// Check to make sure that images are supported in the DOM.
if(document.images){
// Check to see whether you are using a name, number, or object
if (typeof(daImage) == 'string') {
// This whole objStr nonesense is here solely to gain
compatability
// with ie3 for the mac.

objStr = 'document.' + daImage;
obj = eval(objStr);
srcStr = daPath + "nav_" + daImage + "_" + offOn + ".gif";
//srcEval = eval(daPath);
obj.src = srcStr;

objStr2 = 'document.' + daPage;
obj2 = eval(objStr2);

if (offOn == "on") {
srcStr2 = daPath + "nav_" + daPage + "_off.gif";
obj2.src = srcStr2;
} else {
srcStr2 = daPath + "nav_" + daPage + "_on.gif";
obj2.src = srcStr2;
}

} else if ((typeof(daImage) == 'object') && daImage && daImage.src)
{

daImage.src = daPath + "nav_" + daImage + "_" + offOn + ".gif";
if (offOn == "on") {
daPage.src = daPath + "nav_" + daPage + "_off.gif";
} else {
daPage.src = daPath + "nav_" + daPage + "_on.gif";
}
}
}
}


function SimpleSwap(daImage, daSrc){
var objStr,obj;
//alert ("now entering simple swap");
// Check to make sure that images are supported in the DOM.
if(document.images){
// Check to see whether you are using a name, number, or object
if (typeof(daImage) == 'string') {
// This whole objStr nonesense is here solely to gain
compatability
// with ie3 for the mac.
objStr = 'document.' + daImage;
obj = eval(objStr);
obj.src = daSrc;
} else if ((typeof(daImage) == 'object') && daImage && daImage.src)
{
daImage.src = daSrc;
}
}
}
//
</script>
 
M

Michael Winter

[snip]
its just after the body tag
<script language=javascript src=/scripts/swap.js></script>

If this is the exact tag pair, the problem is that the src attribute value
is not quoted. Quotes *must* be used when the value includes characters
that are not among this set:

- Letters
- Numbers
- Underscores (_)
- Periods (.)
- Hyphens (-)
- Colons :))

Furthermore, the type attribute is required for the script element, and
the language attribute is deprecated. The above should read:

<script type="text/javascript" src="/scripts/swap.js"></script>

You could also place it in the HEAD element so that it is guaranteed to be
available to the document.

[snip]

The script below contains unnecessary eval() function calls. You should
modify the code with the alternatives that I show below (comments have
been stripped for clarity).
<script type="text/javascript">
function WM_imageSwap(daImage, offOn){
var daPath = "images/";
var daPage = "home";
if(document.images){
if (typeof(daImage) == 'string') {

objStr = 'document.' + daImage;
obj = eval(objStr);

daImage is a string that contains the name or id of an IMG element,
correct? If so, it should be accessed using the images collection:

obj = document.images[ daImage ];

For future reference, you could have avoided the eval() call using:

obj = document[ daImage ];

If daImage was 'logo', for example, it would be the same as writing:

obj = document.logo;
srcStr = daPath + "nav_" + daImage + "_" + offOn + ".gif";
//srcEval = eval(daPath);

Image.src expects a string, so that eval() should never have been
necessary. The assignment might as well be direct, too: the value isn't
used elsewhere, so the variable is unnecessary.

Both of intermediate variables (objStr and srcStr) have been declared
global unnecessarily. Use the var keyword to keep them local.
obj.src = srcStr;

objStr2 = 'document.' + daPage;
obj2 = eval(objStr2);

This should probably be:

obj2 = document.images[ daPage ];

and if not, it should certainly be:

obj2 = document[ daPage ];

[snipped object branch]
function SimpleSwap(daImage, daSrc){
var objStr,obj;
if(document.images){
if (typeof(daImage) == 'string') {
objStr = 'document.' + daImage;
obj = eval(objStr);

I'm sure you can guess by now how this should be written.
obj.src = daSrc;
} else if ((typeof(daImage) == 'object') && daImage && daImage.src)
{
daImage.src = daSrc;
}
}
}
//

Informative comment! :p
</script>

Hope that helps,
Mike
 
D

David Dorward

comcast said:
at least i think it this is the problem
am trying to fix a webpage/javascript that i didn't write
www.deltaneutral.com when invoked or any new page from the menu, get
the error msg

..js files should contain JavaScript, not a combination of HTML and
JavaScript.

Get rid of the <script> tags from .js files.

(and run the HTML through http://validator.w3.org/ )
 
R

Richard Cornford

<snip>

There are two other common causes of javascript moved to external files
not working in that location when they work form a page. The first is
including HTML in the external file, so if that opening script tag is in
the external file it becomes a syntax error because it is not
javascript. (SGML comment tags are also often syntax errors in external
JS files)

The other is badly configured servers, specifically older apache
versions where files with JS extensions are not associated with a known
content type so the server sends them as text/html (which doesn't matter
in itself) but also inserts an HTML 2.0 doctype, which is again a syntax
error in a javascript file. You don't see this second problem much these
days.

In both cases testing with Mozilla/Gecko browsers usually reveals the
problem as its error messages include the offending line of code, so it
is obvious when it is not javascript (or was not part of the original
file).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top