Use JS variable to set the src attribute for [removed] tag

前端 未结 5 703
后悔当初
后悔当初 2020-12-10 10:11

I want to use a javascript variable as a \'src\' attribute for another tag on the same jsp.



        
5条回答
  •  没有蜡笔的小新
    2020-12-10 11:01

    Try:

    (function(d){
         var file = 'yourJS.js';
         var ref = d.getElementsByTagName('script')[0];
         var js = d.createElement('script');
         js.src = file;
         ref.parentNode.insertBefore(js, ref);
    }(document));
    

    What this does:

    1. Find the first script element on your page
    2. Creates a new script element with your supplied source.
    3. Then inserts that new element before the first existing script element.

提交回复
热议问题