Javascript:在新页面中打开链接并运行脚本 [英] Javascript: Open link in new page and run script

查看:521
本文介绍了Javascript:在新页面中打开链接并运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够打开一个新窗口

I require the ability to open a new window

window.open(url,'_blank');

然后运行javascript脚本,例如

Then run a javascript script such as

window.open(url,'_blank').ready("javascript in here");

但我不知道怎么做,有没有办法可以做到这一点?

But I don't know how to do it, is there a way that I can do this?

描述很简短,但我认为需要说的只是

The description is short but I think that's all that needs to be said

推荐答案

总之,你不能做你想要的。新窗口是沙盒。它只能运行在自己的html文件中引用的javascript。

In short you cannot do what you are asking. The new window is sandboxed. It can only run javascript referenced within its own html file.

一种可能为您提供所需功能的解决方案是使用postMessage。即便如此,为了做到这一点,接收页面需要收听潜在的消息。
https://developer.mozilla.org/en -US / docs / Web / API / Window / postMessage

One possible solution that might get you what you need would be to use postMessage. Even so in order to do this the receiving page needs to be listening to potential messages. https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

另一种方法是在URL中包含一个查询参数并解析出客户端的参数在新窗口中,根据参数的值采取行动。

An alternative would be to include a query param in the url and parse that param out client side in the new window and take action based on the value of the param.

这篇关于Javascript:在新页面中打开链接并运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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