Simplifying script include line

Having problems with DHTML Menu? There is usually somebody here who knows the answer.
Post Reply
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Simplifying script include line

Post by etornick »

Currently, it is required to paste the following lines in the file:

<SCRIPT language=JavaScript src="milonic_src.js" type=text/javascript></SCRIPT>
<script language=JavaScript>
if(ns4)_d.write("<scr"+"ipt language=JavaScript src=mmenuns4.js><\/scr"+"ipt>");
else _d.write("<scr"+"ipt language=JavaScript src=mmenudom.js><\/scr"+"ipt>");
</script>

Does anyone know how this could be reduced to a single line like <SCRIPT language=JavaScript src="file_that_has_it all.js" type=text/javascript></SCRIPT>

My client is interested in purchasing the worldwide license but only if this can be done. This is because they are going to require their clients to insert that line in their html files and they don't want the client to paste the 3 lines.

Thanks,
Ed
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Post by etornick »

This is one solution... But are there namespace clashes between the two menu libraries? I am also loading an extra library when I don't need it.


I put these 3 lines in one file called somename.js .
somename.js

document.write("<script language='JavaScript' src='milonic_src.js' type='text/javascript'></script>");
document.write("<script language=JavaScript src=mmenuns4.js></script>");
document.write("<script language=JavaScript src=mmenudom.js></script>");


Then I include this one file with a line like
<SCRIPT language=JavaScript src="somename.js" type=text/javascript></SCRIPT>


Any thoughts would be appreciated.
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Post by etornick »

On furthur testing it looks like you cant have both libraries included at once. It definitely gets messed up.
User avatar
Maz
Milonic God
Milonic God
Posts: 1717
Joined: Fri Jun 06, 2003 11:39 pm
Location: San Francisco
Contact:

Post by Maz »

We probably need more info on what kind of system this is, if its one main system then yes you would normally only include the 3 source files once and link back to them. Then change the menu_data.js accordingly for each.

Maybe you need to integrate it with the backend.

maz
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Post by etornick »

All I basically need to do is have the include be for a single file and not the 3 lines of script.

If I just put the lines

<SCRIPT language=JavaScript src="milonic_src.js" type=text/javascript></SCRIPT>
<script language=JavaScript>
if(ns4)_d.write("<scr"+"ipt language=JavaScript src=mmenuns4.js><\/scr"+"ipt>");
else _d.write("<scr"+"ipt language=JavaScript src=mmenudom.js><\/scr"+"ipt>");
</script>

into a file and include that file in the head or body I get a script error. It must have something to do with how javascript is loaded and executed.


I tried putting these lines in a file:

document.write("<script language='JavaScript' src='milonic_src.js' type='text/javascript'></script>");
document.write("<script language='JavaScript'>");
document.write("if (ns4) _d.write('<script language=JavaScript src=mmenuns4.js>') </\script> ");
document.write("else _d.write('<script language=JavaScript src=mmenudom.js>') </\script> ");
document.write("</script>");


and then including the file in a single statement.

It almost works.... the "else_d.write( is not being interpreted correctly.

I am certain that someone who knew javascript well would know what I am doing wrong here.

Any javascript gurus out there?

Any help would be appreciated.

Ed
User avatar
John
 Team
 Team
Posts: 5967
Joined: Sun May 19, 2002 8:23 pm
Location: Phoenix, AZ
Contact:

Post by John »

You could use includes. Put the standard JS calls in one file...

Code: Select all

<script language="JavaScript" src="milonic_src.js" type="text/javascript"></script> 
<script language="JavaScript"> 
if(ns4)_d.write("<scr"+"ipt language=JavaScript src=mmenuns4.js><\/scr"+"ipt>"); 
else _d.write("<scr"+"ipt language=JavaScript src=mmenudom.js><\/scr"+"ipt>"); 
</script>
Name this file anything you like - perhaps menu_include.html. I placed mine in the same directory as the menu files - /menu/.

Now, wherever needed, a single include statement will pull all the JS calls...

Code: Select all

<!--#include file="/menu/menu_include.html" -->
The server-relative addressing lets this include be used as is from any directory level.

Note that the exact format of the include statement may need to be adjusted for different server platforms.
John
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Post by etornick »

Hi:

Thanks for your info... but I don't think that will work for me.

What you showed me was a server side include..

My setup is this....

I have a client who provides a service to many clients. Each one of those clients has their own already built web sites.

What these clients will be doing is inserting into their pages a href to my server with a line like....

<SCRIPT src="http://myserver.com/ServletName/somecode"></SCRIPT>

I will be returning to them a specialized menu that will show up in their page when their user mouses over the link in that code I downloaded to their page.

So now I have to require them to paste the 3 lines at the top of their file as well as the include .


One possibility is to have another servlet return the 3 lines...., and a servlet return the menu.... Maybe that is what i should do..
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Post by etornick »

The servlet returning the lines made no difference. I think this is a general javascript issue.

The problem seems to be...

<script language="JavaScript" src="milonic_src.js" type="text/javascript"></script>
<script language="JavaScript">
if(ns4)_d.write("<scr"+"ipt language=JavaScript src=mmenuns4.js><\/scr"+"ipt>");
else _d.write("<scr"+"ipt language=JavaScript src=mmenudom.js><\/scr"+"ipt>");
</script>

if (ns4) assumes that the first script src milonic_src.js is already loaded. What I want to have is the first line executed and expanded, then the next lines execute.


If I put all of these lines in a file and have that file included on the client when the client page loads the value ns4 is unknown... also _d which probably is "document" isn't defined yet.
User avatar
Maz
Milonic God
Milonic God
Posts: 1717
Joined: Fri Jun 06, 2003 11:39 pm
Location: San Francisco
Contact:

Post by Maz »

Sorry about the delay, I'm sure someone will be along shortly, everyone is in different time zones.

Maybe a minor point, but unable to see what is happening, have you defined all the paths correctly? I assume you are using a full url, if not make sure it has a slash / from root on all 3 src paths.

maz
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Post by etornick »

Yes, I am sure that I have the right URL because if I paste the code in directly it will work.

There is only a problem in putting the lines in a file and "including" that file.
etornick
Advanced
Advanced
Posts: 10
Joined: Mon Apr 12, 2004 6:38 pm

Post by etornick »

Just to summarize: I have tried putting these lines in a file called myfile.js:

document.write('<script language="JavaScript" src="milonic_src.js" type="text/javascript"></script> '
+ '<script language="JavaScript"> '
+ 'if(ns4)_d.write("<scr"+"ipt language=JavaScript src=mmenuns4.js><\/scr"+"ipt>"); '
+ 'else _d.write("<scr"+"ipt language=JavaScript src=mmenudom.js><\/scr"+"ipt>"); '
+ '</script>');

And in the html file where the menu will be I have this line:

<SCRIPT language=JavaScript src="myfile.js"
type=text/javascript></SCRIPT>


This results in an error that ns4 is undefined..

I also tried putting this in the file:

document.write("<script language='JavaScript' src='milonic_src.js' type='text/javascript'> </script>");
document.write("<script language='JavaScript' src='mmenudom.js'> </script>");

This works because the test for ns4 is not there and I am just loading the mmenudom.
User avatar
Andy
Milonic
Milonic
Posts: 3308
Joined: Sun May 19, 2002 8:23 pm
Location: Menu Developer
Contact:

Post by Andy »

Hi,

Adding, this code:

Code: Select all

if(ns4)_d.write("<scr"+"ipt language=JavaScript src=mmenuns4.js><\/scr"+"ipt>");  
else _d.write("<scr"+"ipt language=JavaScript src=mmenudom.js><\/scr"+"ipt>"); 
_d.write("<scr"+"ipt language=JavaScript src=menu_data.js><\/scr"+"ipt>");
At the end of milonic_src.js will work

Cheers
Andy
Post Reply