Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > code embedded in html file vs .js file reference

Reply
Thread Tools

code embedded in html file vs .js file reference

 
 
comcast news
Guest
Posts: n/a
 
      03-07-2004
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>



 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      03-07-2004
On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <(E-Mail Removed)>
wrote:

> 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>
>
>
>




--
Michael Winter
http://www.velocityreviews.com/forums/(E-Mail Removed)d (replace ".invalid" with ".uk" to reply)
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      03-07-2004
On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <(E-Mail Removed)>
wrote:

[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!

> </script>


Hope that helps,
Mike

--
Michael Winter
(E-Mail Removed)d (replace ".invalid" with ".uk" to reply)
 
Reply With Quote
 
David Dorward
Guest
Posts: n/a
 
      03-07-2004
comcast news wrote:

> 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/ )


--
David Dorward <http://dorward.me.uk/>
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      03-07-2004
Michael Winter wrote: <(E-Mail Removed)>
<snip>
>> <script type="text/javascript">
>> function WM_imageSwap(daImage, offOn){

<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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML Code embedded within HTML Page as Text =?Utf-8?B?UGF1bA==?= ASP .Net 5 11-07-2007 05:32 PM
Embedded vs. Non-embedded Tests Trans Ruby 11 09-05-2007 11:22 AM
Embedded languages based on early Ada (from "Re: Preferred OS, processor family for running embedded Ada?") Colin Paul Gloster VHDL 48 04-10-2007 10:31 AM
How to display images embedded in e-mail as embedded, not attachments Jim Firefox 4 12-11-2004 05:36 AM
Databind an embedded control in an embedded datagrid Thomas Dodds ASP .Net Datagrid Control 0 07-26-2004 08:20 PM



Advertisments