在新页面加载页面后调用函数 [英] Call function after page was loaded in new tab

查看:123
本文介绍了在新页面加载页面后调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击按钮后,我需要在新选项卡中打开一些链接,并且在页面加载完成后,我需要以编程方式在此页面上执行一些操作(如单击适当的GUI元素等)。



例如,这段代码应该在新标签 http://stackoverflow.com 中打开,然后点击Tags:

 < button type = \button \
onclick = window.open('http://stackoverflow.com','_blank');
document.getElementById('nav-tags')。click();> Click Me!
< / button>

但函数 document.getElementById('nav-tags')。click ()不会调用。

解决方案

正如我在评论中所说的,无法直接在标签页/窗口之间进行通信。



然而,有一些解决方法:


  1. 打开另一个选项卡时,发送你需要的url数据(就像我在评论stackoveflow.com/tags中建议的那样)

  2. 使用cookie进行通信( https://stackoverflow.com/a/4079423/3600886 使用本地存储进行通信( http://dev.w3.org/html5/webstorage/ ) - 这里有一个库: https://github.com/diy/intercom.js/

如果你的用法是在页面加载时显示一些数据,我会说第一个选项,它可以在所有浏览器上运行,没有任何其他限制。

使用其他选项,您需要页面位于同一个域或具有相同的父窗口,而旧版浏览器不支持本地存储。

After clicking on the button i need to open some link in new tab, and than after the page would be loaded, I need to programmatically perform some actions on this page (like clicking on proper GUI elements etc).

For example this code should open in new tab http://stackoverflow.com and after that click on "Tags":

<button type=\"button\"
    onclick="window.open('http://stackoverflow.com', '_blank');
    document.getElementById('nav-tags').click();">Click Me!
</button>

but the function document.getElementById('nav-tags').click() does not call.

解决方案

As I said in comment, there is no way to directly communicate between tabs/windows.

There are way around this however:

  1. when opening another tab, send data you need in url (like i suggested in comments stackoveflow.com/tags)
  2. communicate using cookies (https://stackoverflow.com/a/4079423/3600886)
  3. communicate using local storage (http://dev.w3.org/html5/webstorage/) - there is a library for that: https://github.com/diy/intercom.js/

If your usage is to just show some data upon page load, I'd say go with first option, it will work across all browsers, free of any other restrictions.

With other options you will need pages to be on same domain or have same parent window, plus local storage is not supported by older browsers.

这篇关于在新页面加载页面后调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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