function getCurrentScriptUrl(){ if(document.currentScript){ return document.currentScript.src; //chrome firefox opera 32 }else{ try{ throw Error(); }catch(e){ if(e.sourceURL){ return e.sourceURL;//safari } if(e.stack){ //ie11 ie10 var arrLines = e.stack.split('\n'); var last_line = arrLines[arrLines.length - 1]; //something like this --> at Global code (http://localhost:63342/execrise/t.js:34:1) var arrReg = /.*\((.*)\:\d+\:\d+\)/.exec(last_line); var script_url = arrReg[1]; return script_url; } var head = document.head || document.getElementsByTagName('head')[0]; var scripts = head.getElementsByTagName('script'); if(navigator.userAgent.indexOf('MSIE') != -1){ var script_url = ''; for(var i=0;i<scripts.length;i++){ //ie9 ie8 has the whole url, ie7 ie6 ie5 has relative url if('interactive' === scripts[i].readyState){ script_url = scripts[i].src; break; } }; if(/MSIE 7\.0/.test(navigator.userAgent) || /MSIE 6\.0/.test(navigator.userAgent)){ //ie7, ie6 add suffix to the relative src script_url = getCurrentPath() + script_url; } return script_url; } } } function getCurrentPath(){ var temp_url = 'blah'; var img = document.createElement('img'); img.src = temp_url; // set string url var folder_path = img.src.replace(temp_url, ''); img.src = null; // no server request return folder_path; } } document.write(getCurrentScriptUrl())
get currrent script url
最新推荐文章于 2019-04-01 20:04:03 发布