简体   繁体   中英

How to pass javascript variable to javascript src link

How to pass javascript variable to javascript src link

I want to pass variable myoutput to replace YOUR_LABEL_HERE with variable value

My code:

 <script>
    var myoutput = 'Diet';
</script>
<script src="/feeds/posts/default/-/YOUR_LABEL_HERE?published&amp;alt=json-in-script&amp;callback=labelthumbs" type='text/javascript'> </script>    

Give it an id , which then allows you to target it like any other element, then change its src :

(inspect the snippet's result to see the change)

 const theScript = document.getElementById("changeMySrc"), theVariable = "something" theScript.src = "http://somesite.com/" + theVariable + "/urlEnd"
 <script id="changeMySrc"></script>

The script is now <script id="changeMySrc" src="http://somesite.com/something/urlEnd"></script> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM