By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

Javascript "require_once()" Equivalent?

P: n/a
In PHP, there is a require_once() function which includes a file only
if it is not already included. Is there an equivalent function in
Javascript (one that checks if a .js file has been included)? If not a
straight-up function, is there some other way to check if a javascript
file has been included already?

Dec 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Joel Byrd wrote:
In PHP, there is a require_once() function which includes a file only
if it is not already included. Is there an equivalent function in
Javascript (one that checks if a .js file has been included)?
No.
If not a straight-up function, is there some other way to check if a
javascript file has been included already?


Yes, you could define a special variable or property and check for
that, or you could check the DOM tree for a `script' element with
a special `src' property value.

But then there is no need for a test as loading client-side scripts
dynamically is unreliable and the document author should have control
about its contents.
PointedEars
Dec 15 '05 #2

P: n/a
Thomas 'PointedEars' Lahn said the following on 12/14/2005 7:04 PM:
Joel Byrd wrote:

In PHP, there is a require_once() function which includes a file only
if it is not already included. Is there an equivalent function in
Javascript (one that checks if a .js file has been included)?

No.

If not a straight-up function, is there some other way to check if a
javascript file has been included already?

Yes, you could define a special variable or property and check for
that, or you could check the DOM tree for a `script' element with
a special `src' property value.


But having that special src property value doesn't mean it was loaded.
Create a variable in the .js file and then check for that variable.
But then there is no need for a test as loading client-side scripts
dynamically is unreliable and the document author should have control
about its contents.


And what is so unreliable about dynamically loading .js files as opposed
to loading script files that are hard coded?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 15 '05 #3

P: n/a
Ok, thanks. Yeah, the only reason I was trying to load a client-side
script dynamically is that I was creating a PHP class; and in the
class, I was using the XMLHttpRequest object, and I wanted to include
the sarissa.js wrapper class if it was not already included. The idea
was to make the person using the class have to do as little as possible
(like having to include the sarissa file, or having to copy some other
javascript and paste it into the document in which they're using the
class). But I guess there's a point at which this effort to make it
easy for the user of the class is made at the expense of good, reliable
code.

Dec 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.