473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Javascript "require_once()" Equivalent?

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
3 8969
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
Has anyone here ever done a case where you have a select multiple form element and you have to do both server-side and client-side validation? I am honestly not sure how to do it in Javascript (I...
43
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is...
6
by: Rick | last post by:
Hello all. I have an index.php file that has a lot of functions that I wrote. Let's say for the sake of argument that there are 1000 functions of 100 lines each. The index.php file is invoked...
4
by: vishal | last post by:
i have seen require_once("file_name") at many places. can anyone tell me the use of this??? thxs for help in advance
11
by: Kimmo Laine | last post by:
I'm flipping my wig here, people. I'm using classes and making each class a file. when I'm including dependet classess, I use require_once to avoid multiple declarations - yet they happen. I put...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
1
by: delset | last post by:
This is my first post on this form so please bear with me if i don't give all the needed info. My problem seems simple but i googled for hours and have not yet found anything that can help with my...
2
by: Paul | last post by:
I am moving an existing app written years ago to a new server. It uses Sigma Template 1.3 and Quickform 1.1.1 and PEAR.php,v 1.1.1.1 2004/02/16 The directory structure is like this: /site...
4
by: three-eight-hotel | last post by:
I'm somewhat of a newbie to PHP coding, but have developed a site using the technology, and have been pleasantly surprised by the capabilities offered. I am more comfortable in the ASP world,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.