gpt4 book ai didi

javascript - 如何在与当前窗口不同的上下文中运行脚本?

转载 作者:行者123 更新时间:2023-12-03 09:06:02 25 4
gpt4 key购买 nike

代码:

var dataScript = '$("#foo").append("$ and #foo exists!")';

假设我们在文档中加载了一些库(例如:$jQuery),如果我们运行它

eval(dataScript); //This will work and adds a text to our current document

我想要:

eval(dataScript); //$ is undefined (Maybe you're in another context?)

我目前使用 JSJS 库 http://jterrace.github.io/js.js/但是这个库对于手动添加许多基本项目是必要的,例如 windowdocumentetc ...

是否有一些方法可以在不与当前页面交互的情况下运行脚本? 当前窗口和新上下文之间还应该存在通信。

+

最佳答案

您可以将 Iframe 与沙箱选项一起使用,这对我来说是最好的解决方案 http://www.w3schools.com/tags/att_iframe_sandbox.asp

关于javascript - 如何在与当前窗口不同的上下文中运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32184085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com