javascript 在脚本标签下编写内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9629970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing Content Under Script Tags
提问by Aman Virk
I am trying to create script tags dynamically under my page using javascript. So far I am able to create it, able to set its type and src. Now my question is, is there any way that instead of defining the src to a different page can I assign its content on the same page? Let me write my code to make it make more sense:
我正在尝试使用 javascript 在我的页面下动态创建脚本标签。到目前为止,我能够创建它,能够设置它的类型和 src。现在我的问题是,有什么方法可以代替将 src 定义到不同的页面,而是将其内容分配到同一页面上吗?让我写我的代码让它更有意义:
var script = document.createElement("script");
script.type = "text/javascript";
script.src = 'custom.js';
Now is there any way I can assign it the content as well by doing something like this:
现在有什么方法可以通过执行以下操作来为其分配内容:
script.content = 'document.write("stackoverflow")';
script.html = 'document.write("stackoverflow")';
I am not sure whether they exist or not, but just guessing if i can do something like this.
我不确定它们是否存在,但只是猜测我是否可以做这样的事情。
回答by RobG
You can do:
你可以做:
var script_tag = document.createElement('script');
script_tag.type = 'text/javascript';
script_tag.text = 'alert("hello world")';
document.body.appendChild(script_tag);
In practice, whether you set the type property or not is likely irrelevant, but it gives one a warm inner glow.
在实践中,是否设置 type 属性可能无关紧要,但它会给人一种温暖的内在光芒。
回答by KyleCPM
Scripts can be read from or written to using 'scripts.text'. It's part of the script Data Object Model (DOM). And for some reason or another, it is different than other HTML tags. Example: 'myScript.innerHTML' tends not to work, but 'scripts.namedItem("myScript").text' does.
可以使用“scripts.text”读取或写入脚本。它是脚本数据对象模型 (DOM) 的一部分。出于某种原因,它与其他 HTML 标签不同。示例: 'myScript.innerHTML' 往往不起作用,但 'scripts.namedItem("myScript").text' 可以。
<p>Change scripts.text <a href="https://www.w3schools.com/jsref/prop_script_text.asp">https://www.w3schools.com/jsref/prop_script_text.asp</a>
</p>
<p>Script Object <a href="https://www.w3schools.com/jsref/dom_obj_script.asp">https://www.w3schools.com/jsref/dom_obj_script.asp</a>
</p>
<canvas id="Canvas1" style="border: 5px solid cyan"></canvas>
<p id="Para1">...</p>
<button onclick="ChangeScript()">Change Script</button>
<button onclick="DrawIt()">Drawit</button>
<button onclick="para1.innerHTML=script1.text;">Show Script</button>
<script id="Script1"></script>
<script>
function ChangeScript() {
script1.text = "function DrawIt(){canvas1.fillRect(1,2,30,40)}"
}
//Note: changing the script text more than once may not work.
canvas1 = document.getElementById("Canvas1").getContext("2d");
script1 = document.scripts.namedItem("Script1");
para1 = document.getElementById("Para1")
</script>
<a href="http://www.w3schools.com/code/tryit.asp?filename=FCR1ZI3MBN77">Try it Yourself, Change scripts.text</a>