Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

External JS from external JS file 2

Status
Not open for further replies.

bbruggemeyer

Programmer
Nov 14, 2001
16
0
0
GB
Hi all

I am using an external .js file to dynamically link several more .js files into an HTML page. This is a great technique for being able to switch code elements without having to edit hundreds of static HTML pages (Can't use server-side!)

The problem occurs with Netscape browsers. It seems to work fine in the majority of cases but on some pages the Netscape parser seems to give up half way through the main HTML page, even though the View Source window shows that all code has been downloaded.

Has anyone tried a similar technique and found it successful or am I trying to do too much with the browser?

Technique is as follows...

HTML page includes
Code:
<SCRIPT language=&quot;javascript&quot; src=&quot;all_code.js&quot;></SCRIPT>
<script language=&quot;javascript&quot;>
<!--
    // Page specific javascript, including...
    document.title = myTitle.toUpperCase() + &quot; - Summary&quot;;
//-->
</script>

all_code.js includes
Code:
document.writeln('<SCRIPT Language=&quot;javascript&quot; src=&quot;module1.js&quot;></SCRIPT>');
document.writeln('<SCRIPT Language=&quot;javascript&quot; src=&quot;module2.js&quot;></SCRIPT>');
document.writeln('<SCRIPT Language=&quot;javascript&quot; src=&quot;module3.js&quot;></SCRIPT>');

example of error
Code:
unterminated string literal. 

    document.title = myTitle.toUpperCase() + &quot; - Summ
.............................................^

Line from view source
Code:
    document.title = myTitle.toUpperCase() + &quot; - Summary&quot;;
 
hi
the technique is good, & it have to be working, couze almost everyone use it..
you have to cleen up your codes:

1. you shouldn't write this way:
document.write(&quot;<script .. ></script>&quot;), becouse the closing &quot;</script>&quot; tag could close your current script section where you're trying to include the second script file got what i mean? use
document.write(&quot;<script..></sc&quot;+&quot;ript>&quot;); or something similar

2. unterminated string literal - the only thing that comes to my mind is that you might have a line break there (the hole statement have to be on the single line)

let us know if this would do the job :) Victor
 
Thanks for the response Victor,

Won't be able to try this 'til monday morning (UK time) 'cos it's all at the office. Sounds good though. I'll post the result then.

Bill

Bill Bruggemeyer
&quot;It ain't what you do, it's the way that you do it!&quot;
 
I'm afraid the suggestion made no difference. Having changed the code I get the same problem.

The significant factor regarding the &quot;unterminated string literal&quot; error is that it is caused by something else. If I pad the code before with extra spaces or newlines I can move the location of the error such that a different error is generated.

The errors generated do look (as Victor suggests) as though a line break is being inserted in the code at a position related to the length of the HTML page. However, if I move the code from the first .JS file into the script block in the .HTML file these errors do not then occur.

I'm puzzled!!!

Bill Bruggemeyer
&quot;It ain't what you do, it's the way that you do it!&quot;
 
can't tell anything on it now, but there is another thing:

sometimes, with netscape (v.4.x most of times) when you (or me) have some function in external file & call it from that file
/something like:

function aaa(){
...
}

aaa();
/
then this function doesn't executes, & you have to call it from main page onload, but if you won't do that, you may have some variables undefined, blah blah..

hope you got what i meant
Victor
 
Yep, I'm aware of that one. But, unfortunately, not relevant here. The problem is with the parsing and we haven't executed anything yet (apart from a few document.write()s.)

Thanks for your help anyway Victor. I've now cross-posted to the Netscape forum to see if I can get any answers there. Are there any other resources I should be checking out?
Bill Bruggemeyer
 
Hey Bill, other resources, well search for forums it brought me to this one, and i think it rocks, you get your answers fast and in 9 of 10 times the responses help very much (in my case)

but try google, good luck with searching

=) Pornstar
 
hi guyz
well, don't know, try may be there would be someone who faced something similar to your problem

there are a lot of forums on the NET, just keep trying to find the solution yourself, & you'll find it :)

good luck, Victor
 
Thanks Pornstar, Victor

If I do find an answer I'll be sure to post it back here.

Bill Bruggemeyer
 
Whenever using Javascript to write Javascript, you should split up your beginning AND ending tags. Also, you might want to use standard C-style quoting, and encode your line breaks into the commands:
Code:
document.write(&quot;<SCR&quot; + &quot;IPT Language=\&quot;javascript\&quot; src=\&quot;module1.js\&quot;></SCR&quot; + &quot;IPT>\n&quot;);
document.write(&quot;<SCR&quot; + &quot;IPT Language=\&quot;javascript\&quot; src=\&quot;module2.js\&quot;></SCR&quot; + &quot;IPT>\n&quot;);
document.write(&quot;<SCR&quot; + &quot;IPT Language=\&quot;javascript\&quot; src=\&quot;module3.js\&quot;></SCR&quot; + &quot;IPT>\n&quot;);

Anyway, it looks to me like somewhere you have a small problem with quoting and escaping.

Also, try moving your &quot;page-specific&quot; script block to another place. I often put things like that at the end of the page, after the </body> tag.

Are those external .js files writing any of the main HTML in the page? If so, you might have a conflict there. Sometimes, one .js file might load before another one. It doesn't necessarily happen in the order you want it to. So, if one script is depending on something that the other script should have already written, you might have a problem.
 
Thanks for the latest tips. I've tried splitting the opening script tags and switching to c-style string delimiters and it didn't make any difference.

As far as I'm aware there are no dependencies that could be out of synch, but there's rather a lot of code being used so I'll have to trawl through it all. It may well be that this is the issue.

I've got rather a lot on at the moment so Netscape compatability has just been pushed down the priority list again. I'll post back when I've had a chance to give it a complete examination.

Thanks again,
headbang.gif
Bill Bruggemeyer
 
Which Netscape is having this problem, version 4.x or version 6?

Is there a chance that you have other <script> tags anywhere in your .js files?

Another thing I have noticed with Netscape/IE is that Netscape is sometimes a little more picky about case in calling Javascript methods and handlers. For example, in some versions of Netscape, &quot;onmousover&quot; didn't work, but &quot;onMouseOver&quot; did.

Whenever you're ready, I would be glad to try your script in my browsers.
 
Bill, take care of your head, you'll need it in the future, trust me :)

rycamor, yea, i've also noticed that captureEvents(Event.mousedown) won't work in several cases (say, if you've captured this event in a layer, & have another layer inside this one, wich has same sizes(so called DUMMY layer))
but captureEvents(Event.MOUSEDOWN) would do the job

netscape.. Victor
 
Hi Chaps,

I've searched all my code for strings containing the script tag and split them all to avoid problems. In answer to rycamor's question this is only happening with Netscape 4.x browsers, although I'm nowhere near moving to Netscape 6 yet.

You've both got some interesting points regarding event handling but I'm pretty sure it's not relevant to this problem.

One very interesting fact that I've just discovered is that the number of newline characters inserted in the HTML page corresponds directly to the number of document.writeln statements I've used.

I have a page where line-breaks are being added into the HTML section, and appear on the view source. The correct source should be... (&NBSP; in caps for this message only)
Code:
 <TD colspan=&quot;3&quot; height=&quot;30&quot;>&NBSP;
    <IMG
etc...

and the view source shows me
Code:
 <TD colspan=&quot;3&quot; height=&quot;30&quot;> &







nbsp;
    <IMG
and actually rendering the
Code:
&NBSP;
as the characters &quot;& nbsp;&quot;

The main html page includes a .js file which has 8 document.writelns linking to other files. This corresponds to the number of newlines inserted in the
Code:
&NBSP;
entity. If I comment-out a number of these document.writelns the number of newlines is reduced by the same amount.

external .js file
Code:
var myType = 'medi';
var myFolder = '0001';

document.writeln('<LI'+'NK href=&quot;../styles/pdx.css&quot; rel=&quot;styleSheet&quot; type=&quot;text/css&quot;>');
document.writeln('<LI'+'NK href=&quot;../styles/pictureviewer.css&quot; rel=&quot;styleSheet&quot; type=&quot;text/css&quot;>');
document.writeln('<SC'+'RIPT Language=&quot;javascript&quot; src=&quot;../scripts/button.js&quot;></SCR'+'IPT>');
document.writeln('<SC'+'RIPT Language=&quot;javascript&quot; src=&quot;../scripts/navigate.js&quot;></SCR'+'IPT>');
document.writeln('<SC'+'RIPT Language=&quot;javascript&quot; src=&quot;../scripts/domains.js&quot;></SCR'+'IPT>');
document.writeln('<SC'+'RIPT Language=&quot;javascript&quot; src=&quot;../scripts/cookie.js&quot;></SCR'+'IPT>');
document.writeln('<SC'+'RIPT Language=&quot;javascript&quot; src=&quot;../scripts/protect.js&quot;></SCR'+'IPT>');
document.writeln('<SC'+'RIPT Language=&quot;javascript&quot; src=&quot;../us_' + myType + '/' + myFolder + '/pic_data.js&quot;></SCR'+'IPT>');

Having found this out I'm not too sure which direction to take next. Now my head really hurts!!!


headbang.gif
Bill Bruggemeyer
 
I am experiencing this problem too. We are using coolmenus.js ( served up by our SilverStream app server ( Netscape 4.7 renders just enough of the page to display everthing, except that the flyout menus fail to render issuing an unterminated string or other such mysterious JavaScript error. Looking at the web server's console, I see that doing a View|Source or File|Save in the browser actually re-requests the entire page!

Our pages are actually Java servlets compiled by SilverStream's proprietary design environment, thus we really don't have control over exactly what HTML is transmitted to the browser. It is peppered with unmatched paragraph tags and loads of unexpected whitespace. The extra whitespace could be new line characters, for all I know.

Are there any published limits on how much HTML Netscape 4.7 can process before it stops reading embedded JavaScript?

Thanks [in advance] for any help.
 
I have discovered that huge complex pages can be rendered by javascript, if invoked by a single
Code:
.write()
statement.

Consider my navigable cross-browser calendar component at
Once the supporting 26K dynamicHTML3.js library is cached, only
Code:
(new Date()).calendar().write()
is needed to render the entire calendar. Type
Code:
javascript:(new Date()).calendar().source().write()
into the address box to see the calendar clientlet's HTML!
 
I too have encountered this problem across a slew of pages that I've been developing. Each page has a document.write in the header in order to call a browser-specific style sheet. For some reason, the line-breaking problem occurs only on some of the pages, and intermittently when it does occur.

I got a lot out of this thread. (Namely, the knowledge that I'm not crazy, and Netscape 4 is actually destroying my code. Even as I clearly saw the source code in front of me, I could not believe my eyes!) What I'm wondering is whether or not there is any solid information on this apparant bug anywhere on the internet. I've searched around and found the two threads posted on this forum and nothing else. It seems like too big of a deal to be so mysterious.....

Thanks for any info in advance!

Spot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top