473,513 Members | 2,533 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code embedded in html file vs .js file reference

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>

Jul 20 '05 #1
4 4757
On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <bx******@comcast.net>
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
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2
On Sun, 07 Mar 2004 08:05:27 GMT, comcast news <bx******@comcast.net>
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! :P
</script>


Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3
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/>
Jul 20 '05 #4
Michael Winter wrote: <op**************@news-text.blueyonder.co.uk>
<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.
Jul 20 '05 #5

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

Similar topics

2
3539
by: Aquarius2431 | last post by:
Hi!, I don't think I have posted to this group before. Have been using PHP on my webserver for a few months now and finding that I like it quite a bit. Here is a question that just occurred to me. I recently created a BBS (Bulletin Board Service) on my website where I allow people to post messages via a form. It just occurred to me that...
242
13136
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on...
16
2015
by: graham.reeds | last post by:
I am updating a website that uses a countdown script embedded on the page. When the page is served the var's are set to how long the countdown has left in minutes and seconds, but the rest of the script is left untouched. However I want to take the script out of the page and have it as a seperate file that can be cached, reducing serving...
171
7596
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles) like it because you can split the code from the design. That is logical. But if you are the only one working on the code, it seem a little...
2
4988
by: SAL | last post by:
I would like to create a VB.net function, that builds a dynamic hyperlink using System.Web.UI.WebControls.HyperLink, but I can not find any examples on how to generate a dynamic hyperlink. Has anyone done this before? I do not want to dynamically create my hyperlink on an ASP page, but rather a behind the scenes link that a user can paste...
0
5104
by: wulongtea | last post by:
I am trying generate an HTML email message with C# Visual Studio 2003 that has a graphics file embedded in the message. (I know i should be using 2005 and the new classes there, but I cannot upgrade yet due to the policy at my company) my function seems pretty simple. but what i get is an HTML email that has an attached file, whic his the...
24
1497
by: Skijor | last post by:
I have inherited a php script that nests php, javascript, and html. How do I clean up code like this: I think I can move the javascript into a seperate .js file at the least. What about the nesting of php inside the html? <?php ....snip... function ShowCart()
1
2111
by: Peter Rilling | last post by:
Hi. I have an interesting problem and don't know if there is a solution to this. I have some webcontrols that are compiled into assemblies (not usercontrols). Some of these controls rely on Javascript file which are included as embedded resources where the HTML reference is being echoed by using the...
0
1814
by: Formula | last post by:
Hello everybody,because I am newbie in python two weeks only but I had programming in another languages but the python take my heart there's 3 kind of arrays Wow now I hate JAVA :) . I am working now on html process. So, I found a good class for getting html tag but I don't know how to use it I wrote this code for getting the tag A hopping...
0
7397
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7565
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5103
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4759
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3255
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
1612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
817
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.