Forum Moderators: phranque

Message Too Old, No Replies

Getting a Java to run on External Script

         

carfac

3:21 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi:

For those of you bored with the google dance...

I want to post a java script in an external file, and call it from the page. I can do this sometimes, and I can't get it to work sometimes, so I figure there must be a trick I am missing...

In the page itself, I use this:

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript" SRC="/menu.js">

</SCRIPT>

And then for the script, I save it text, uplaod binary, set permission 755, and start it with a "<!-- " and end it with a "//-->".

Well, it does not seem to be running, but I do not know what else I need to do!

Thanks!

Oh, and while we are on the subject of JAVA acripts, can I combine two small java scripts (one to block right-click, one to force a reload if in a frame) into one externat file? Just pop one in after the other? Or should I make it two calls?

Thanks!

dave

jdMorgan

3:55 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



carfac,

Remove the leading slash from "menu.js"

<script Language="JavaScript" type="text/JavaScript" src="menu.js"> </script>

You should be able to just glue two scripts together and call them by one src name without any problems caused by having "two scripts."

Jim

carfac

4:10 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jd:

Thanks- I owe you. A couple. If you ever get to Utah, let me know, I'll buy you a few! (Note: contrary to popular opinion, it IS possible to get a drink in Utah. On Fridays this month, I think it is!)

dave

jdMorgan

4:49 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dave,

Hey, no problem - If I ever travel in that area, I'll look you up - Bound to be thirsty after a long trip... ;)

Jim

dcheney

4:54 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One small point of clarification. Despite the similar names "Java" and "JavaScript" are completely different unrelated technologies. The discussion above is completely about "JavaScript".

carfac

5:06 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dcheney:

I did not know that! What is Java?

dave

dcheney

5:13 am on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


The official home of Java is: http://java.sun.com/

JavaScript is really ECMA-262, located here: http://www.ecma.ch/ecma1/STAND/ECMA-262.HTM

andreasfriedrich

12:08 pm on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



start it with a "<!-- " and end it with a "//-->".

Thatīs not neccessary, in fact may even be wrong in external JavaScript files. The comments were neccessary to hide the script from old browsers that did not understand the script element and would therefor try to render the its content. If those old browsers encounter a script element that references an external file the will still ignore the script element and try to render its content. Since there is no content there is no need for a comment. Since they just ignore the script element they wonīt load the external file.

Remove the leading slash from "menu.js"

A relative URI in the form "/menu.js" is called an absolute path reference. It is a valid URI that is just resolved differently than the more commonly known relative path reference such as "menu.js". In the former case when building the absolute URI scheme and authority (Remember an URI has these components: <scheme>://<authority><path>?<query>) are inherited from the base URI whereas in the latter case the path info is inherited (simply concatenated) as well and is then normalized.

Suppose [domain.tld...] would point to the "menu.js" file. Then you could use ...src="/menu.js"... in any document with any path within your domain (http://www.domain.tld/scary/little/chaps.html or [domain.tld...] It would always work without having to do anything about the path. This is quite useful when all your JavaScript or CSS files reside in directories (which are mapped to the corresponding URI of the same name) like /js or /css.

Andreas

jdMorgan

2:37 pm on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



andreasfriedrich,

Yes, I like to think of the prepended-slash (e.g, "/menu.js") path form as simply saying, "Go to the site's top-level directory, and start there."

In this case, I assumed carfac's script was in the same directory as the page calling it. I should have said, "Try removing the leading slash", but my keyboard got away from me... :)

Thanks for the info on the proper naming of this form of URI.

carfac,

Did you get it working?

Jim

carfac

6:48 pm on Sep 28, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi:

I got it working, and by removing the slash... but they are definitely NOT in the same directory. All the pages that call the js are from a dynamic script, one directory down... that is:

root:
index.html
menu.js
etc

/subdir/:
page.cgi (that generates the page calling menu.js)

All I know is that it works now, so I am ok!

dave