简体   繁体   English

试图将javascript注入我的页面

[英]Trying to inject javascript into my page

For reasons too complicated to get into now, I have an ajax call that returns some dynamically created Javascript that I want to inject into my page. 由于太复杂而无法进入的原因,我有一个ajax调用,它返回一些动态创建的Javascript,我想将其注入到我的页面中。 The following code works on Chrome, but not in IE: 以下代码适用于Chrome,但不适用于IE:

 var node = document.getElementsByTagName("head")[0] || document.body;
  if (node)
  {
    var script = document.createElement("script");
    script.type = "text/javascript";
    //script.innerHTML = json.javascript;
    var textnode = document.createTextNode(json.javascript);
    script.appendChild(textnode);
    node.appendChild(script);
  }

In IE, I get "SCRIPT65535: Unexpected call to method or property access." 在IE中,我得到“SCRIPT65535:对方法或属性访问的意外调用。” As you can see from the commented out code, before I tried the textnode, I tried just inserting it with script.innerHTML. 从注释掉的代码中可以看出,在我尝试使用textnode之前,我尝试使用script.innerHTML插入它。 That also worked in Chrome, but in IE I got "SCRIPT600:Unknown runtime error". 这也适用于Chrome,但在IE中我得到了“SCRIPT600:未知的运行时错误”。

Is there a way to stick some javascript into the DOM in IE? 有没有办法在IE中将一些javascript粘贴到DOM中?

And of course, as soon as I post this, I find http://www.phpied.com/dynamic-script-and-style-elements-in-ie/ 当然,只要我发布这个,我就会找到http://www.phpied.com/dynamic-script-and-style-elements-in-ie/

  var node = document.getElementsByTagName("head")[0] || document.body;
  if (node)
  {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.text = json.javascript;
    node.appendChild(script);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试学习JavaScript,但我的页面没有任何作用 - Trying to learn JavaScript and nothing is working on my page Javascript 重定向并注入新页面 - Javascript redirect and inject into the new page 使用Python将Javascript注入网页 - Inject Javascript into web page with Python 量角器-将JavaScript注入测试页面 - Protractor - inject JavaScript into tested page 将 Javascript 代码注入 web 页面 - Inject Javascript code into a web page 试图让我的Resolve注入我的控制器,以便能够注入HTML - Trying to get my Resolve to inject into my controller to be able to inject into the HTML 如何在我的UIWebview中注入javascript - How to inject javascript in my UIWebview 尝试使用 mockttp 将 JavaScript 注入网页 - Trying to Inject a JavaScript into webpage using mockttp 我可以将哪些 JavaScript/CSS 注入到我的 WebView 中以仅反转页面的背景和白色文本? - What JavaScript/CSS can I inject into my WebView to invert only the background of the page and white text? ASP.NET:是否可以在我的Web应用程序的每个页面中注入CSS或Javascript? - ASP.NET: Is it possible to inject CSS or Javascript into every page in my web app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM