最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call function after page was loaded in new tab - Stack Overflow

programmeradmin2浏览0评论

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 and after that click on "Tags":

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

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

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. and after that click on "Tags":

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

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

Share Improve this question edited Jan 8, 2015 at 8:26 royhowie 11.2k14 gold badges53 silver badges67 bronze badges asked Jan 8, 2015 at 7:57 walterwalter 3095 silver badges16 bronze badges 4
  • there is no way of doing this (for security reasons) but what you could do is load stackoverflow./tags – avidenic Commented Jan 8, 2015 at 8:02
  • It's just example with stackoverflow. . These needs for SPA (single page application) website – walter Commented Jan 8, 2015 at 8:09
  • If SPA is made with angular, you can still open direct url. Other ways of doing this are: using cookies to store/load information between tabs or local storage. But they must be in same domain/have same parent. – avidenic Commented Jan 8, 2015 at 8:15
  • Possible duplicate of Call a JavaScript function across browser tabs – Ivar Commented Sep 18, 2017 at 19:45
Add a ment  | 

3 Answers 3

Reset to default 2

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

There are way around this however:

  1. when opening another tab, send data you need in url (like i suggested in ments stackoveflow./tags)
  2. municate using cookies (https://stackoverflow./a/4079423/3600886)
  3. municate using local storage (http://dev.w3/html5/webstorage/) - there is a library for that: https://github./diy/inter.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.

Create the tab from JavaScript and you can access the tab with JavaScript and place a onload function

var w = window.open();
w.onload = function(){

};

You can also put the onload function in the tab:

window.onload = function(){
}

You need to write your JS in .ready event. So it will be called when whole page DOM is ready:

$(document).ready(function () {
      document.getElementById('nav-tags').click();
});
发布评论

评论列表(0)

  1. 暂无评论