Как подключить javascript файл из своего js скрипта
Пример подключения скрипта http://example.com/script.js:
Native javascript
var script = document.createElement('script');
script.src = 'http://example.com/script.js';
document.getElementsByTagName('head')[0].appendChild(script);
alert('script loaded');
jQuery:
$.getScript('http://example.com/script.js', function(){
alert('script loaded');
});
JavaScript strval function
A JavaScript equivalent of PHP’s strval
function strval (str) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// % note 1: Comment out the entire switch if you want JS-like behavior instead of PHP behavior
// - depends on: gettype
// * example 1: strval({red: 1, green: 2, blue: 3, white: 4});
// * returns 1: 'Array'
var type = '';
if (str === null) {
return '';
}
type = this.gettype(str);
switch (type) {
case 'boolean':
if (str === true) {
return '1';
}
return '';
case 'array':
return 'Array';
case 'object':
return 'Object';
}
return str;
}
Example 1
strval({red: 1, green: 2, blue: 3, white: 4});
Should return
'Array'
JavaScript print_r function
A JavaScript equivalent of PHP’s print_r
function print_r (array, return_val) {
// http://kevin.vanzonneveld.net
// + original by: Michael White (http://getsprink.com)
// + improved by: Ben Bryan
// + input by: Brett Zamir (http://brett-zamir.me)
// + improved by: Brett Zamir (http://brett-zamir.me)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// - depends on: echo
// * example 1: print_r(1, true);
// * returns 1: 1
var output = '',
pad_char = ' ',
pad_val = 4,
d = this.window.document,
getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
},
repeat_char = function (len, pad_char) {
var str = '';
for (var i = 0; i < len; i++) {
str += pad_char;
}
return str;
},
formatArray = function (obj, cur_depth, pad_val, pad_char) {
if (cur_depth > 0) {
cur_depth++;
}
var base_pad = repeat_char(pad_val * cur_depth, pad_char);
var thick_pad = repeat_char(pad_val * (cur_depth + 1), pad_char);
var str = '';
if (typeof obj === 'object' && obj !== null && obj.constructor && getFuncName(obj.constructor) !== 'PHPJS_Resource') {
str += 'Array\n' + base_pad + '(\n';
for (var key in obj) {
if (Object.prototype.toString.call(obj[key]) === '[object Array]') {
str += thick_pad + '[' + key + '] => ' + formatArray(obj[key], cur_depth + 1, pad_val, pad_char);
}
else {
str += thick_pad + '[' + key + '] => ' + obj[key] + '\n';
}
}
str += base_pad + ')\n';
}
else if (obj === null || obj === undefined) {
str = '';
}
else { // for our "resource" class
str = obj.toString();
}
return str;
};
output = formatArray(array, 0, pad_val, pad_char);
if (return_val !== true) {
if (d.body) {
this.echo(output);
}
else {
try {
d = XULDocument; // We're in XUL, so appending as plain text won't work; trigger an error out of XUL
this.echo('<pre xmlns="http://www.w3.org/1999/xhtml" style="white-space:pre;">' + output + '</pre>');
} catch (e) {
this.echo(output); // Outputting as plain text may work in some plain XML
}
}
return true;
}
return output;
}
Example 1
print_r(1, true);
Should return
1
JavaScript isset function
A JavaScript equivalent of PHP’s isset
function isset () {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: FremyCompany
// + improved by: Onno Marsman
// + improved by: Rafał Kukawski
// * example 1: isset( undefined, true);
// * returns 1: false
// * example 2: isset( 'Kevin van Zonneveld' );
// * returns 2: true
var a = arguments,
l = a.length,
i = 0,
undef;
if (l === 0) {
throw new Error('Empty isset');
}
while (i !== l) {
if (a[i] === undef || a[i] === null) {
return false;
}
i++;
}
return true;
}
Example 1
isset( undefined, true);
Should return
false
Example 2
isset( 'Kevin van Zonneveld' );
Should return
true
JavaScript is_unicode function
A JavaScript equivalent of PHP’s is_unicode
function is_unicode (vr) {
// + original by: Brett Zamir (http://brett-zamir.me)
// % note 1: Almost all strings in JavaScript should be Unicode
// * example 1: is_unicode('We the peoples of the United Nations...!');
// * returns 1: true
if (typeof vr !== 'string') {
return false;
}
// If surrogates occur outside of high-low pairs, then this is not Unicode
var arr = [],
any = '([\s\S])',
highSurrogate = '[\uD800-\uDBFF]',
lowSurrogate = '[\uDC00-\uDFFF]',
highSurrogateBeforeAny = new RegExp(highSurrogate + any, 'g'),
lowSurrogateAfterAny = new RegExp(any + lowSurrogate, 'g'),
singleLowSurrogate = new RegExp('^' + lowSurrogate + '$'),
singleHighSurrogate = new RegExp('^' + highSurrogate + '$');
while ((arr = highSurrogateBeforeAny.exec(vr)) !== null) {
if (!arr[1] || !arr[1].match(singleLowSurrogate)) { // If high not followed by low surrogate
return false;
}
}
while ((arr = lowSurrogateAfterAny.exec(vr)) !== null) {
if (!arr[1] || !arr[1].match(singleHighSurrogate)) { // If low not preceded by high surrogate
return false;
}
}
return true;
}
Example 1
is_unicode('We the peoples of the United Nations...!');
Should return
true
JavaScript is_string function
A JavaScript equivalent of PHP’s is_string
function is_string (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: is_string('23');
// * returns 1: true
// * example 2: is_string(23.5);
// * returns 2: false
return (typeof mixed_var === 'string');
}
Example 1
is_string('23');
Should return
true
Example 2
is_string(23.5);
Should return
false
JavaScript is_scalar function
A JavaScript equivalent of PHP’s is_scalar
function is_scalar (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Paulo Freitas
// * example 1: is_scalar(186.31);
// * returns 1: true
// * example 2: is_scalar({0: 'Kevin van Zonneveld'});
// * returns 2: false
return (/boolean|number|string/).test(typeof mixed_var);
}
Example 1
is_scalar(186.31);
Should return
true
Example 2
is_scalar({0: 'Kevin van Zonneveld'});
Should return
false
JavaScript is_resource function
A JavaScript equivalent of PHP’s is_resource
function is_resource (handle) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// + improved by: Luis Salazar (http://www.freaky-media.com/)
// * example 1: is_resource('a');
// * returns 1: false
var getFuncName = function (fn) {
var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
if (!name) {
return '(Anonymous)';
}
return name[1];
};
return !(!handle || typeof handle !== 'object' || !handle.constructor || getFuncName(handle.constructor) !== 'PHPJS_Resource');
}
Example 1
is_resource('a');
Should return
false
JavaScript is_real function
A JavaScript equivalent of PHP’s is_real
function is_real (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// - depends on: is_float
// % note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
// % note 1: it different from the PHP implementation. We can't fix this unfortunately.
// * example 1: is_double(186.31);
// * returns 1: true
return this.is_float(mixed_var);
}
Example 1
is_double(186.31);
Should return
true
JavaScript is_object function
A JavaScript equivalent of PHP’s is_object
function is_object (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Legaev Andrey
// + improved by: Michael White (http://getsprink.com)
// * example 1: is_object('23');
// * returns 1: false
// * example 2: is_object({foo: 'bar'});
// * returns 2: true
// * example 3: is_object(null);
// * returns 3: false
if (Object.prototype.toString.call(mixed_var) === '[object Array]') {
return false;
}
return mixed_var !== null && typeof mixed_var === 'object';
}
Example 1
is_object('23');
Should return
false
Example 2
is_object({foo: 'bar'});
Should return
true
Example 3
is_object(null);
Should return
false
JavaScript is_numeric function
A JavaScript equivalent of PHP’s is_numeric
function is_numeric (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: David
// + improved by: taith
// + bugfixed by: Tim de Koning
// + bugfixed by: WebDevHobo (http://webdevhobo.blogspot.com/)
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: is_numeric(186.31);
// * returns 1: true
// * example 2: is_numeric('Kevin van Zonneveld');
// * returns 2: false
// * example 3: is_numeric('+186.31e2');
// * returns 3: true
// * example 4: is_numeric('');
// * returns 4: false
// * example 4: is_numeric([]);
// * returns 4: false
return (typeof mixed_var === 'number' || typeof mixed_var === 'string') && mixed_var !== '' && !isNaN(mixed_var);
}
Example 1
is_numeric(186.31);
Should return
true
Example 2
is_numeric('Kevin van Zonneveld');
Should return
false
Example 3
is_numeric('+186.31e2');
Should return
true
JavaScript is_null function
A JavaScript equivalent of PHP’s is_null
function is_null (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: is_null('23');
// * returns 1: false
// * example 2: is_null(null);
// * returns 2: true
return (mixed_var === null);
}
Example 1
is_null('23');
Should return
false
Example 2
is_null(null);
Should return
true
JavaScript is_long function
A JavaScript equivalent of PHP’s is_long
function is_long (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Paulo Freitas
// - depends on: is_float
// % note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
// % note 1: it different from the PHP implementation. We can't fix this unfortunately.
// * example 1: is_long(186.31);
// * returns 1: true
return this.is_float(mixed_var);
}
Example 1
is_long(186.31);
Should return
true
JavaScript is_integer function
A JavaScript equivalent of PHP’s is_integer
function is_integer (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Paulo Freitas
// - depends on: is_int
// % note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
// % note 1: it different from the PHP implementation. We can't fix this unfortunately.
// * example 1: is_integer(186.31);
// * returns 1: false
// * example 2: is_integer(12);
// * returns 2: true
return this.is_int(mixed_var);
}
Example 1
is_integer(186.31);
Should return
false
Example 2
is_integer(12);
Should return
true
JavaScript is_int function
A JavaScript equivalent of PHP’s is_int
function is_int (mixed_var) {
// http://kevin.vanzonneveld.net
// + original by: Alex
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + revised by: Matt Bradley
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: WebDevHobo (http://webdevhobo.blogspot.com/)
// + improved by: Rafał Kukawski (http://blog.kukawski.pl)
// % note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
// % note 1: it different from the PHP implementation. We can't fix this unfortunately.
// * example 1: is_int(23)
// * returns 1: true
// * example 2: is_int('23')
// * returns 2: false
// * example 3: is_int(23.5)
// * returns 3: false
// * example 4: is_int(true)
// * returns 4: false
return mixed_var === +mixed_var && isFinite(mixed_var) && !(mixed_var % 1);
}
Example 1
is_int(23)
Should return
true
Example 2
is_int('23')
Should return
false
Example 3
is_int(23.5)
Should return
false