script-tag

Dynamically added script will not execute

早过忘川 提交于 2019-11-27 14:48:04
I am dynamically adding a script of a github gist. If I added it to the html before the page loads, the script will execute. But If I try to add it to the page after it loads it adds the script to the page but doesn't execute it. Here is how I am adding it to the page Html <div id="results"></div> Javascript $('#results').click(function() { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "https://gist.github.com/1265374.js?file=GetGists.js"; document.getElementById('results').appendChild(script); console.log('script added'); }); Here is the live

When should <script> tags be visible and why can they?

半世苍凉 提交于 2019-11-27 09:42:26
问题 A script element that got styled as display:block appears visible. Why is it possible and is there any real use case where it is desired? td > * { display: block; } <table> <tr> <td> <script type="text/javascript"> var test = 1; </script>von 1 </td> </tr> </table> 回答1: The HTML5 specification defines a style sheet that user agents (like browsers) are expected to use. Section 10.3.1 lists the styles for "Hidden elements": @namespace url(http://www.w3.org/1999/xhtml); [hidden], area, base,

React: Script tag not working when inserted using dangerouslySetInnerHTML

只谈情不闲聊 提交于 2019-11-27 04:49:32
I'm trying to set html sent from my server to show inside a div using dangerouslySetInnerHTML property in React. I also have script tag inside it and use functions defined in same inside that html. I have made example of error in JSFiddle here . This is test code: var x = '<html><scr'+'ipt>alert("this.is.sparta");function pClicked() {console.log("p is clicked");}</scr'+'ipt><body><p onClick="pClicked()">Hello</p></body></html>'; var Hello = React.createClass({ displayName: 'Hello', render: function() { return (<div dangerouslySetInnerHTML={{__html: x}} />); } }); I checked and the script tag

How can I read a JSON in the script-tag from JavaScript?

女生的网名这么多〃 提交于 2019-11-27 03:31:10
I have a dynamically generated page where I want to use a static JavaScript and pass it a JSON string as a parameter. I have seen this approach used by Google (see Google's +1 Button: How do they do it? ). But how should I read the JSON string from the JavaScript? <html> <head> <script src="jquery-1.6.2.min.js"></script> <script src="myscript.js">{"org": 10, "items":["one","two"]}</script> </head> <body> Hello </body> </html> In this JavaScript I would like to use the JSON argument {"org": 10, "items":["one","two"]} from the HTML document. I don't know if it's best to do it with jQuery or

Get content inside script as text

丶灬走出姿态 提交于 2019-11-27 02:04:46
问题 I would like to print the content of a script tag is that possible with jquery? index.html <script type="text/javascript"> function sendRequest(uri, handler) { } </script> Code alert($("script")[0].???); result function sendRequest(uri, handler) { } 回答1: Just give your script tag an id: <div></div> <script id='script' type='text/javascript'> $('div').html($('#script').html()); </script> ​ http://jsfiddle.net/UBw44/ 回答2: You can use native Javascript to do this! This will print the content of

multiple versus single script tags

亡梦爱人 提交于 2019-11-26 19:26:43
问题 Is there any difference (performance, best practices, etc) between using a single script tag with embedded code in it, or using multiple script tags with the same code spread all over the HTML? For example: <script> foo(); </script> ... <script> bar(); </script> versus: <script> foo(); bar(); </script> Thanks 回答1: 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

HTML/Javascript: how to access JSON data loaded in a script tag with src set

人走茶凉 提交于 2019-11-26 17:27:26
I have this JSON file I generate in the server I want to make accessible on the client as the page is viewable. Basically what I want to achieve is: I have the following tag declared in my html document: <script id="test" type="application/json" src="http://myresources/stuf.json"> The file referred in its source has JSON data. As I've seen, data has been downloaded, just like it happens with the scripts. Now, how do I access it in Javascript? I've tried accessing the script tag, with and without jQuery, using a multitude of methods to try to get my JSON data, but somehow this doesn't work.

How to pass parameters to a Script tag?

ε祈祈猫儿з 提交于 2019-11-26 17:08:36
I read the tutorial: http://drnicwilliams.com/2006/11/21/diy-widgets/ for XSS Widgets by Dr. Nic. I'm looking for a way to pass parameters to the script tag. For example, to make the following work: <script src="http://path/to/widget.js?param_a=1&param_b=3"> Is there a way to do this? UPDATE: Two interesting links: How to embed Javascript widget that depends on jQuery into an unknown environment (Stackoverflow discussion) An article on passing parameters to a script tag Richard Fox I apologise for replying to a super old question but after spending an hour wrestling with the above solutions I

Dynamically added script will not execute

送分小仙女□ 提交于 2019-11-26 16:56:35
问题 I am dynamically adding a script of a github gist. If I added it to the html before the page loads, the script will execute. But If I try to add it to the page after it loads it adds the script to the page but doesn't execute it. Here is how I am adding it to the page Html <div id="results"></div> Javascript $('#results').click(function() { var script = document.createElement("script"); script.type = "text/javascript"; script.src = "https://gist.github.com/1265374.js?file=GetGists.js";

How to tell if a <script> tag failed to load

亡梦爱人 提交于 2019-11-26 12:06:33
I'm dynamically adding <script> tags to a page's <head> , and I'd like to be able to tell whether the loading failed in some way -- a 404, a script error in the loaded script, whatever. In Firefox, this works: var script_tag = document.createElement('script'); script_tag.setAttribute('type', 'text/javascript'); script_tag.setAttribute('src', 'http://fail.org/nonexistant.js'); script_tag.onerror = function() { alert("Loading failed!"); } document.getElementsByTagName('head')[0].appendChild(script_tag); However, this doesn't work in IE or Safari. Does anyone know of a way to make this work in