Forum Moderators: phranque
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
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
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
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