在新窗口中运行 Javascript.open

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32357312/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:09:19  来源:igfitidea点击:

Running Javascript in new window.open

javascriptjqueryhtml

提问by user3333134

I'm running this function to open a new window.

我正在运行此功能以打开一个新窗口。

function htmlNewWindow(id) {
    var html = $(id).html();
    var newWindow = window.open('');
    newWindow.document.body.innerHTML =  '<html><head><title>Hi</title>  <script src="js/myScript.js"></script> </head>' + html;    
}

This successfully creates a new window with the HTML in it. I have a bunch of HTML tags which when clicked run a function called Foo1. I've tried printing the entire function of Foo1 to the new HTML document, and tried putting Foo1 inside myScript.js. I see both Foo1 inside a script tag in the new window, and but neither are loaded since they are just written to the new page as HTML.

这成功地创建了一个包含 HTML 的新窗口。我有一堆 HTML 标签,单击它们时会运行一个名为 Foo1 的函数。我尝试将 Foo1 的整个函数打印到新的 HTML 文档中,并尝试将 Foo1 放入 myScript.js。我在新窗口的脚本标签中看到了 Foo1,但都没有加载,因为它们只是作为 HTML 写入新页面。

回答by Barmar

Scripts added with .innerHTMLaren't executed. You need to create a scriptnode and append it to the window's DOM.

.innerHTML不执行添加的脚本。您需要创建一个script节点并将其附加到窗口的 DOM。

$("#button").click(newWindow);

function newWindow(id) {
  var html = $(id).html();
  var win = window.open('');
  win.document.head.innerHTML = '<title>Hi</title></head>';
  win.document.body.innerHTML = '<body>' + html + '</body>';
  var script = document.createElement('script');
  script.src = 'js/myScript.js';
  win.document.head.appendChild(script);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">Click me</button>

This doesn't run in Stack Snippet's sandbox, here's a working jsfiddle.

这不在 Stack Snippet 的沙箱中运行,这是一个有效的jsfiddle

回答by LostMyGlasses

Try this:

试试这个:

var newWindow = window.open('');
newWindow.document.createElement('script');
script.src = 'js/myScript.js';
newWindow.document.head.appendChild(script);

回答by Mike Horstmann

Here's how you create, and then append a script file within a new window:

以下是您创建并在新窗口中附加脚本文件的方法:

var fileref = document.createElement('script');
//creates script in current document
fileref.setAttribute("type", "text/javascript")
//set it to JS by "type"
fileref.setAttribute("src", filename)
//set your "src=yourFile_href_Here.js" 


//Then create your newWindow as you did above, but slightly updated
//Create your function which will consume the "fileref" argument
function htmlNewWindow(fileref) {
    var newWindow = window.open('');
    newWindow.document.getElementsByTagName("head")[0].appendChild(fileref);
}; //right now the function is made but you still have to execute it

//Execute your function, and pass it the variable "fileref" that you set above.    

htmlNewWindow(fileref);
//Within this edit you will append the head element
//with your newly created script(or any other parameterized argument)

/* Replace your filename to pass any other script */

NOTE - Opening a page residing on a different domain, if not specifically allowed, will reject instances of this due to CORS(https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)

注意 - 打开位于不同域中的页面,如果没有特别允许,将因 CORS(https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)而拒绝这种情况

It's not a safe practice to be sending your scripts into other people's pages or allowing them in your own if your domain hasn't sent them. Also, depending on your server/technology stack you may need to configure your *-origin settings within your backend stack. See here: (https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy)

如果您的域没有发送脚本,将脚本发送到其他人的页面或允许它们进入您自己的页面并不是一种安全的做法。此外,根据您的服务器/技术堆栈,您可能需要在后端堆栈中配置 *-origin 设置。请参阅此处:(https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

回答by Andrés Villenas

Just in case someone has this to be done in a link. Do the following:

以防万一有人要在链接中完成此操作。请执行下列操作:

<a href="javascript: var n= window.open('/url/to/page/in/SAMEDOMAIN'); n.focus(); n.addEventListener('load', n.alert('replace this with a good thing'), true);">Link</a>

This opens a new window with that URL, it set the focus to that windows, and as soon as the 'load' event is triggered, it executes the code in the function. It only works with a page in the same domain.

这将打开一个带有该 URL 的新窗口,它将焦点设置到该窗口,并且一旦触发 'load' 事件,它就会执行函数中的代码。它仅适用于同一域中的页面。

Hope this helps ??.

希望这可以帮助 ??。

Cheers

干杯