I'm currently searching a solution to not include two times the same .js
So my question is relatively simple, i'm searching something like requice_once (.php) or #ifndef(.c/.c++).
Are they solution for this ?
require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.
To include the Require. js file, you need to add the script tag in the html file. Within the script tag, add the data-main attribute to load the module. This can be taken as the main entry point to your application.
The require() function is used to include a PHP file into another irrespective of whether the file is included before or not. The require_once() will first check whether a file is already included or not and if it is already included then it will not include it again.
The require_once keyword is used to embed PHP code from another file. If the file is not found, a fatal error is thrown and the program stops. If the file was already included previously, this statement will not include it again.
I would suggest that you use either require.js or head.js for this kind of thing. They are fully-featured and provide performance benefits as well.
You can write your own ifndef-code, just write your files like this (assuming it's for a browser):
if (!window.NAME_UNIQUE_FOR_THIS_FILE) {
window.NAME_UNIQUE_FOR_THIS_FILE = true;
.. your code here ...
}
The if-statement checks if the uniquely named variable exists already. If not, create it (so the next time this code is seen, it will be ignored). Then run whatever you want this file to do.
Be sure to use a very unique name though, since it's a global variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With