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
function require (filename) {
    // !No description available for require. @php.js developers: Please update the function summary text file.
    // 
    // version: 1001.2911
    // 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)
    // %        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 js_code = this.file_get_contents(filename);
    var script_block = d.createElementNS ? 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 ? d.getElementsByTagNameNS('http://www.w3.org/1999/xhtml', 'head')[0].appendChild(script_block) : 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

No comments yet. Be the first!


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: Anonymous and Shawn Houser.
<your name here>

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