使用Javascript刷新当前打开的选项卡

使用Javascript刷新当前打开的选项卡

本文介绍了打开新选项卡时,使用Javascript刷新当前打开的选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过单击第一个选项卡中的链接,是否可以在打开新选项卡时刷新当前打开的选项卡?
基本上,最终结果是这样的:第一个选项卡是打开的,单击第一个选项卡中打开的页面上的链接(使用鼠标中键或类似的东西,只是为了打开它在第二个选项卡中),在第二个选项卡中打开单击的链接,然后自动刷新第一个选项卡。我希望我解释它是可以理解的。注意:我不是在设计我自己的网站,我想用Greasemonkey为网站制作一个插件。

Would it be possible to refresh the currently open tab when a new tab is opened by clicking a link in the first tab?Basically, the end result would be like this: the first tab is open, a link on the page which is open in the first tab is clicked (with middle mouse button or something similar to that, just to make it open in the second tab), link which was clicked is opened in the second tab and then the first tab is automatically refreshed. I hope that I explained it understandably. Note: I'm not designing my own website, I'm looking to make a plugin for the website with Greasemonkey.

推荐答案

假设您的页面上有此链接:

Let's assume you have this link on your page:

<a href="http://server.com/url-to-second-tab" target="_new">Link which opens new tab and refreshes current one</a>

在js脚本中你添加这个(假设你正在使用jQuery,如你所说):

In js script you add this (assuming you are using jQuery, as you stated):

$(document).ready(function(){
   $('body').on('click','a',function(e){
        e.preventDefault()
        // Open new tab - in old browsers, it opens a popup window
        window.open($(this).attr('href'), '_blank');
        // reload current page
        location.reload();
   })
})

请在所有浏览器中测试,因为在某些浏览器中它可能会将链接打开为弹出窗口而不是新标签。

Please test it in all browsers, as in some browsers it may open the link as a popup window instead of a new tab.

作为快速测试,在Chrome上使用F12开发人员工具打开,并在控制台中写入:

As a fast test, open with F12 developer tools on Chrome, and write in console:

window.open('https://google.com', '_blank');location.reload();

这篇关于打开新选项卡时,使用Javascript刷新当前打开的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:32