Create references to external scipt files from within an external script file

Create references to external scipt files from within an external script file

Post by Mellow Cro » Sat, 05 Nov 2005 10:21:05


Just discovered this technique. Is this old hat? Would there be any
disadvantage to doing this?

In your external .js file:

/*
Summary: includes external scripts in this external script
so that you don't have to reference them within the (x)html
document
files.
Remarks:
In effect this inserts something like
<script type="text/javascript"
src="referencingExternalLibrariesB.js"></script>
into the html document.
Param:
scriptUrl (String)
an absolute or relative url.
Usage: In your external scripts, at the top of your documents,
include("file:///C:/temp/myScript.js");
include("otherScript");
*/
function include(scriptSrc) {
var headElement = document.getElementsByTagName("head")[0];
var scriptElement = document.createElement("script");
scriptElement.setAttribute("type", "text/javascript");
scriptElement.setAttribute("src", scriptSrc);
headElement.appendChild(scriptElement);
}

Full example online:
< http://www.yqcomputer.com/
ml>
 
 
 

Create references to external scipt files from within an external script file

Post by Richard Co » Sat, 05 Nov 2005 11:30:59


It is not old hat as such because the number of browsers that fully
support the technique only recently increased to the point where it
could be considered viable in an Internet context. It has been known,
and experimented with, for some considerable time.

<snip>

Yes, you will be excluding some browsers from being able to use the
script without necessarily having a good reason for doing so.

Richard.

 
 
 

Create references to external scipt files from within an external script file

Post by Mellow Cro » Sat, 05 Nov 2005 20:27:23

Richard, both of your points are helpful, thanks.




Yes. It's a W3C/standards/"modern" technique so it's new in that sense.


Yes. That's true. Raises the fundamental issue of whether you should design
for graceful degradation or simply ignore older browsers. I'm sure this has
been thrashed out elsewhere.

May have found a second potential disadvantage.
When testing your page from served local web server (not file system) AND
you use the technique of this thread AND
you are referencing a script file from outside the root of your domain,
using the file system (eg file:///C:/Data/...)
THEN
You get errors :(
 
 
 

Create references to external scipt files from within an external script file

Post by Thomas 'Po » Sat, 05 Nov 2005 21:01:26


Works as designed.


PointedEars
 
 
 

Create references to external scipt files from within an external script file

Post by Randy Web » Sat, 05 Nov 2005 21:05:52

Mellow Crow said the following on 11/4/2005 6:27 AM:



Security errors is what they should be. It is because you are on the
server localhost (or similar) and you are pulling data from the HD. No
different than the page being on www.sampleDomain.com and trying to get
data from your HD. The solution? Put your script files on the "server"
and address them that way.

--
Randy
comp.lang.javascript FAQ - http://www.yqcomputer.com/
Javascript Best Practices - http://www.yqcomputer.com/
 
 
 

Create references to external scipt files from within an external script file

Post by Randy Web » Sat, 05 Nov 2005 21:10:30

Mellow Crow said the following on 11/3/2005 8:21 PM:


Define "old hat" and the age it requires to be considered old hat. As
for the technique of loading JS files on the fly, it is not new. Nowhere
close to new.

<URL:
http://www.yqcomputer.com/ %2Fgroup%2Fcomp.lang.javascript%2Fbrowse_frm%2Fthread%2Fe8d87fd395689d91%2F38548fc341a4e616%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26rnum%3D4%26prev%3D%2Fgroups%3Fq%3Ddynamically%2Bload%2Bjs%2Bfile%2Bgroup:comp.lang.javascript%26hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.javascript%26scoring%3Dd%26
>

Search the archives for "Dynamically loading a .js file" and you will
find the above thread in case the URL gets broken. That article is 2 1/2
years old and I was not the first one to start using that method.

--
Randy
comp.lang.javascript FAQ - http://www.yqcomputer.com/
Javascript Best Practices - http://www.yqcomputer.com/
 
 
 

Create references to external scipt files from within an external script file

Post by Richard Co » Sat, 05 Nov 2005 22:16:37


Browser scripting is about ten years old, and the oldest W3C DOM
standards about 6 years old. How modern is modern?


Those are not the applicable either/ors in this situation. The most
modern, dynamic, DOM standard and capable web browser can have scripting
turned off. So designing for clean degradation should be necessary in
all open public contexts in order to accommodate even modern browsers in
certain (legitimate and possible) configurations.

But browsers are increasingly appearing on small mobile devices. These
browsers are modern (and more or less standards compliant), but not
necessarily nearly as dynamic as desktop browsers. It may be, and has
been, argued that the need for clean degradation in Internet scripts is
currently increasing as a result.

<snip>

It has, but there is no good reason not to state a case and see what
happens. Designing for clean-degradation is very important to the
subject of browser scripting with javascript.

Richard.