简体   繁体   English

包括html + javascript和javascript

[英]Including html+javascript with javascript

I'm working off these two existing links, trying to combine the strategies listed here. 我正在研究这两个现有链接,试图结合此处列出的策略。

How do I include a JavaScript file in another JavaScript file? 如何在另一个JavaScript文件中包含一个JavaScript文件?

Execute my jQuery script after dynamically inserted jQuery library has finished loading 动态插入的jQuery库完成加载后执行我的jQuery脚本

I've boiled my issue down to a simple script that demonstrates the issue I'm having. 我将问题归结为一个简单的脚本,该脚本演示了我遇到的问题。 I have these two files: 我有这两个文件:

test2.html: test2.html:

<script type="text/javascript">alert('test');</script>
foreign html;

test1.html: test1.html:

<script type="text/javascript" language="javascript">
var xmlhttp;

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET", "test2.html", true);
    xmlhttp.send();
</script>
<div id="myDiv">my div</div>

So obviously test1.html is supposed to make an ajax call out to test2.html, and write it's HTML into the div. 因此,显然test1.html应该对test2.html进行ajax调用,并将其HTML写入div。 When I run test1.html, it does this - the div shows "foreign html" which is coming from test2.html - however it doesn't execute the alert box. 当我运行test1.html时,它会执行此操作-div会显示来自test2.html的“外国html”-但是它不会执行警报框。 Visiting test2.html directly does display the alert box. 直接访问test2.html确实会显示警报框。

So in other words, I need to grab the contents of a remote webpage, and insert it into a div, AND have it's javascript executed. 因此,换句话说,我需要获取远程网页的内容,并将其插入div中,并执行javascript。 The "real" test2.html will use jquery, so test1.html cannot use it otherwise I'm loading jquery twice. “真实的” test2.html将使用jquery,因此test1.html无法使用它,否则我将两次加载jquery。

================================================== ==================================================

Based on the comments warning me about XSS & suggesting I change my architecture, I'm editing this to elaborate. 基于警告我有关XSS的评论并建议我更改体系结构,我正在对其进行详细编辑。 The goal of this project is a "piece" of html my users can paste on their sites, this pulls in a bunch of functionality. 这个项目的目标是我的用户可以在他们的站点上粘贴HTML的“一部分”,这带来了很多功能。 The functionality they are pulling in involves loading jquery plugins, and HTML. 他们引入的功能包括加载jquery插件和HTML。 Yes, my users would have to trust me to not XSS attack them. 是的,我的用户必须相信我不要XSS攻击他们。 Just like you trust Google not to XSS you when you use Google Analytics. 就像您相信Google在使用Google Analytics(分析)时不要XSS。 My post isn't asking for security advice. 我的帖子不是要寻求安全建议。 I just want to make this simple "hello world" type example work. 我只想使这个简单的“ hello world”类型示例工作。 The real application is secure. 真正的应用程序是安全的。

When I used jquery in test1.html, it did cause the JS in test2.html to be executed, but this is just a contrived example which is overly simplified compared to my real app. 当我在test1.html中使用jquery时,确实导致了test2.html中的JS被执行,但这只是一个人为的示例,与我的实际应用程序相比,它过于简化。 The real app uses heavy jquery in test2.html - and I need to be able to update test2.html on my server, without giving my users new versions of test1.html. 真正的应用程序在test2.html中使用了繁重的jquery-我需要能够在服务器上更新test2.html,而不必为用户提供新版本的test1.html。 Having test1.html load it's own version of jquery is a lot overhead, just to include test2.html. 仅将test2.html包含在内,将test1.html加载为其自己的jquery版本会产生很多开销。

if you really want to do this you can wrap your js into a div and then use eval() to execute javascript once you've loaded the content of test2.html 如果您确实想执行此操作,则可以将js包装到div中,然后在加载test2.html的内容后使用eval()执行javascript
eg 例如

eval(document.getElementById('script').innerHTML);

you should however consider changing the architecture of your application and use call back functions instead 但是,您应该考虑更改应用程序的体系结构,而改用回调函数
i'd also recommend using a library like jquery since that simplifies ajax calls and call back functions a lot 我还建议您使用类似jquery的库,因为这样可以简化ajax调用和回调函数
another thing i noticed: you should wrap your javascript into an onload function. 我注意到的另一件事:您应该将javascript包装到onload函数中。 if you don't do that you might get a javascript error as you're trying to add content to a div that potentially doesn't exist at the moment you call the function 如果不这样做,则可能会导致JavaScript错误,因为您试图将内容添加到div上,而该内容在调用函数时可能不存在

for security reasons you should not use eval ... 出于安全原因,您不应使用eval ...

use 采用

(new Function(document.getElementById('script').innerHTML))()

https://stackoverflow.com/a/19711866/2450730 https://stackoverflow.com/a/19711866/2450730

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM