Get content inside script as text
Just give your script tag an id: <div></div> <script id=’script’ type=”text/javascript”> $(‘div’).html($(‘#script’).html()); </script> http://jsfiddle.net/UBw44/
Just give your script tag an id: <div></div> <script id=’script’ type=”text/javascript”> $(‘div’).html($(‘#script’).html()); </script> http://jsfiddle.net/UBw44/
For modern browsers that support html5 you can use document.currentScript to get the current script element. Otherwise, use querySelector() to select your script element by id or attribute. Note that we don’t use the src attribute because that can be fragile if you’re delivering over a CDN or with differences between development and production environments. … Read more
If you insert </script> inside a script tag, no matter if it’s a string in quotes, apostrophes, or even a template literal, it will always close the script tag. You have to escape it, for example like that: var str=` <script> <\/script> ` var pre = document.createElement(‘pre’) pre.textContent = str document.body.appendChild(pre) However, if you use … Read more
I created a React component that works pretty much like dangerouslySetInnerHtml but additionally it executes all the js code that it finds on the html string, check it out, it might help you: https://www.npmjs.com/package/dangerously-set-html-content
With inline script like what you quoted, there’s unlikely to be much difference; however, every time the browser’s HTML parser encounters a script tag, it: Comes to a screeching halt Builds up a string of the the text in the tag up until the first time it sees the string “</script>” Hands that text off … Read more
YES, you can 🙂 The answer is: Content Security Policy (CSP). Most modern browsers support this flag, which tells the browser only to load JavaScript code from a trusted external file and disallow all internal JavaScript code! The only downside is, you can not use any inline JavaScript in your whole page (not only for … Read more
How about adding a new script tag to <head> with the script to (re)load? Something like below: <script> function load_js() { var head= document.getElementsByTagName(‘head’)[0]; var script= document.createElement(‘script’); script.src=”https://stackoverflow.com/questions/9642205/source_file.js”; head.appendChild(script); } load_js(); </script> The main point is inserting a new script tag — you can remove the old one without consequence. You may need to add … Read more
You can’t load JSON like that, sorry. I know you’re thinking “why I can’t I just use src here? I’ve seen stuff like this…”: <script id=”myJson” type=”application/json”> { name: ‘Foo’ } </script> <script type=”text/javascript”> $(function() { var x = JSON.parse($(‘#myJson’).html()); alert(x.name); //Foo }); </script> … well to put it simply, that was just the script … Read more
You can put the script into a separate file, then use $.getScript to load and run it. Example: $.getScript(“test.js”, function(){ alert(“Running test.js”); });
I apologise for replying to a super old question but after spending an hour wrestling with the above solutions I opted for simpler stuff. <script src=”..” one=”1″ two=”2″></script> Inside above script: document.currentScript.getAttribute(‘one’); // 1 document.currentScript.getAttribute(‘two’); // 2 Much easier than jQuery or URL parsing. You might need the polyfill for document.currentScript from @Yared Rodriguez’s answer … Read more