get currrent script url

Posted yang_chuanlong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了get currrent script url相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

postman Pre-request Script 发送请求

html 返回登录前页面

python3 投票

jQuery $.get(url,data,callback,type) 返回值给全局变量赋值的问题

基于 JSON 回调数据的重定向 URL

自己动手写waf指纹识别