Use PHP functions in JavaScript

JavaScript require

!No description available for require. @php.js developers: Please update the function summary text file.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
function require (filename) {
    // !No description available for require. @php.js developers: Please update the function summary text file.
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/require    // +   original by: Michael White (http://getsprink.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Yen-Wei Liu    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Force Javascript execution to pause until the file is loaded. Usually causes failure if the file never loads. ( Use sparingly! )
    // %        note 2: Uses global: php_js to keep track of included files
    // -    depends on: file_get_contents
    // *     example 1: require('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');    // *     returns 1: 2
    var d = this.window.document;
    var isXML = d.documentElement.nodeName !== 'HTML' || !d.write; // Latter is for silly comprehensiveness
    var js_code = this.file_get_contents(filename);
    var script_block = d.createElementNS && isXML ? d.createElementNS('http://www.w3.org/1999/xhtml', 'script') : d.createElement('script');    script_block.type = 'text/javascript';
    var client_pc = navigator.userAgent.toLowerCase();
    if ((client_pc.indexOf('msie') !== -1) && (client_pc.indexOf('opera') === -1)) {
        script_block.text = js_code;
    } else {        script_block.appendChild(d.createTextNode(js_code));
    }
 
    if (typeof(script_block) !== 'undefined') {
        d.getElementsByTagNameNS && isXML ? (d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0] ? d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(script_block) : d.documentElement.insertBefore(script_block, d.documentElement.firstChild) // in case of XUL        ) : d.getElementsByTagName('head')[0].appendChild(script_block);
 
        // save include state for reference by include_once and require_once()
        var cur_file = {};
        cur_file[this.window.location.href] = 1; 
        // BEGIN REDUNDANT
        this.php_js = this.php_js || {};
        // END REDUNDANT
        if (!this.php_js.includes) {            this.php_js.includes = cur_file;
        }
 
        if (!this.php_js.includes[filename]) {
            this.php_js.includes[filename] = 1;            return 1;
        } else {
            return ++this.php_js.includes[filename];
        }
    }    return 0;
}
external links: original PHP docs | raw js source

Examples

Running

1
require('http://www.phpjs.org/js/phpjs/_supporters/pj_test_supportfile_2.js');

Should return

1
2

Dependencies

In order to use this function, you also need:

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have require goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Brett Zamir
22 Apr '10 Permalink

q  @Yen-Wei Liu: Thank you for the report! I've made some fixes in http://github.com/kvz/phpjs/raw/master/functions/language/require.js (and also a change for include()) which I believe should fix the issue. Our code was insufficient on two counts: the issue you brought up (though in your fix based on our earlier code, the script would I think actually be added above the head rather than inside it) and the fact that we really should be checking whether this is text/html or true XHTML or XML (since even XHTML served as text/html will have the problem you indicated) in order to make the decision, since as you pointed out, FF possessing the method does not mean it is suitable for the document.

Gravatar
Yen-Wei Liu
22 Apr '10 Permalink

q  on line 27:
d.getElementsByTagNameNS ? d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(script_block) : d.getElementsByTagName('head')[0].appendChild(script_block);

This doesn't run on some versions of browsers, such as FF 3.0 and FF 3.5.

These browsers have getElementsByTagNameNS() but only work with XHTML files, so they never get the "head" tag and
'[0]' would trigger an error/exception.

I suggest this should be fixed like that in include() , where I think someone has noticed this problem :

Change it to :

d.getElementsByTagNameNS ?
(d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0] ?
d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(script_block) :
d.documentElement.insertBefore(script_block, d.documentElement.firstChild)
):
d.getElementsByTagName('head')[0].appendChild(script_block);


Contribute a New function

More functions

In this category

include
include_once
» require
require_once

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !