In Javascript, is it possible to pass a variable into [removed] “src” parameter?

后端 未结 4 1843
萌比男神i
萌比男神i 2021-01-18 18:42

Is it possible in Javascript to pass a variable through the src parameter? ie.



        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-18 19:14

    I use the following pattern to convert query variables from to an object containing key:value pairs. Code is placed at the top of script.js:

        var getVars = {};
        
        (function(){
            var scripts, currentScript, queryString;
    
            scripts = document.getElementsByTagName('script');
            currentScript = scripts[ scripts.length - 1 ];
            queryString = currentScript.getAttribute('src').split("?").pop().split('&');
            for(var i=0;i

    This probably won't work with deferred / asynchronously added script elements, as it relies on immediate code execution.

提交回复
热议问题