如何告诉动态插入的< script>标签到...运行 [英] How can I tell a dynamically inserted <script> tag to… run

查看:108
本文介绍了如何告诉动态插入的< script>标签到...运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态地插入< script> 标签与 src 属性,没有内容。但浏览器不会拉下src并在插入后运行该脚本 - 标签只是位于DOM中。

I'm dynamically inserting a <script> tag with a src attribute and no content. But the browser does not pull down that src and run the script after the insertion -- the tag just sits there in the DOM.

我可以告诉浏览器运行脚本标签?

Is it possible for me to tell the browser to "run" the script tag?

由于我正在使用的其他代码,所以我可以通过 src 属性,而不是自己抓取它并将其插入到标签的正文中 - 但是如果有必要,我也可以这样做(并且欢迎任何建议)。

Because of the other code I'm working with, it's easier for me to keep the code fetched via the src attribute than to fetch it myself and insert it into the body of the tag -- but if that's necessary, I can do that too (and welcome any advice on that).

更新请求的信息


  1. 脚本标记基于在页面加载后,任意次数的用户交互

  2. 我插入了这样的标签(jquery的html功能删除脚本标签):文档。 getElementById(my-div)innerHTML =堆栈溢出想要剥离的脚本标签;

  1. The script tag is inserted based on user interaction an arbitrary number of times after the page has loaded
  2. I'm inserted the tag like this (jquery's html function strips out script tags): document.getElementById("my-div").innerHTML = "the script tag, which stack overflow wants to strip";


推荐答案

请尝试以下代码::它的工作国王

Try following code:: Its working

  var script = document.createElement( 'script' );
  script.type = 'text/javascript'; 
  script.src =MY_URL; 
  $("#YOUR_ELEMNT_ID").append( script );

这篇关于如何告诉动态插入的&lt; script&gt;标签到...运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆