Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old November 4th, 2005, 01:36 AM
Mellow Crow
Guest
 
Posts: n/a
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.softmake.com.au/webScript...alLibraries.ht
ml>


  #2  
Old November 4th, 2005, 02:47 AM
Richard Cornford
Guest
 
Posts: n/a

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


Mellow Crow wrote:[color=blue]
> Just discovered this technique. Is this old hat?[/color]

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.
[color=blue]
> Would there be any
> disadvantage to doing this?[/color]
<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.


  #3  
Old November 4th, 2005, 12:05 PM
Mellow Crow
Guest
 
Posts: n/a

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


Richard, both of your points are helpful, thanks.

Richard Cornford wrote:[color=blue]
> Mellow Crow wrote:[color=green]
>> Just discovered this technique. Is this old hat?[/color]
>
> 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.[/color]

Yes. It's a W3C/standards/"modern" technique so it's new in that sense.
[color=blue][color=green]
>> Would there be any
>> disadvantage to doing this?[/color]
> <snip>
>
> Yes, you will be excluding some browsers from being able to use the
> script without necessarily having a good reason for doing so.[/color]

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 :(



  #4  
Old November 4th, 2005, 12:15 PM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a

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


Mellow Crow wrote:
[color=blue]
> 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 :([/color]

Works as designed.


PointedEars
  #5  
Old November 4th, 2005, 12:15 PM
Randy Webb
Guest
 
Posts: n/a

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


Mellow Crow said the following on 11/4/2005 6:27 AM:[color=blue]
> Richard, both of your points are helpful, thanks.
>
> Richard Cornford wrote:
>[color=green]
>>Mellow Crow wrote:
>>[color=darkred]
>>>Just discovered this technique. Is this old hat?[/color]
>>
>>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.[/color]
>
>
> Yes. It's a W3C/standards/"modern" technique so it's new in that sense.
>
>[color=green][color=darkred]
>>>Would there be any
>>>disadvantage to doing this?[/color]
>>
>><snip>
>>
>>Yes, you will be excluding some browsers from being able to use the
>>script without necessarily having a good reason for doing so.[/color]
>
>
> 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 :([/color]

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://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
  #6  
Old November 4th, 2005, 12:15 PM
Randy Webb
Guest
 
Posts: n/a

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


Mellow Crow said the following on 11/3/2005 8:21 PM:
[color=blue]
> Just discovered this technique. Is this old hat? Would there be any
> disadvantage to doing this?[/color]

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://groups.google.com/group/comp....scoring%3Dd%26[color=blue]
>[/color]

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://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
  #7  
Old November 4th, 2005, 01:25 PM
Richard Cornford
Guest
 
Posts: n/a

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


Mellow Crow wrote:[color=blue]
> Richard Cornford wrote:[color=green]
>> Mellow Crow wrote:[color=darkred]
>>> Just discovered this technique. Is this old hat?[/color]
>>
>> 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.[/color]
>
> Yes. It's a W3C/standards/"modern" technique so it's new in that
> sense.[/color]

Browser scripting is about ten years old, and the oldest W3C DOM
standards about 6 years old. How modern is modern?
[color=blue][color=green][color=darkred]
>>> Would there be any
>>> disadvantage to doing this?[/color]
>> <snip>
>>
>> Yes, you will be excluding some browsers from being able to
>> use the script without necessarily having a good reason for
>> doing so.[/color]
>
> Yes. That's true. Raises the fundamental issue of whether
> you should design for graceful degradation or simply ignore
> older browsers.[/color]

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.
[color=blue]
> I'm sure this has been thrashed out elsewhere.[/color]
<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.


Closed Thread