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

编程入门 行业动态 更新时间:2024-10-25 04:26:49
本文介绍了打开新选项卡时,使用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="server/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('google', '_blank');location.reload();

更多推荐

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

本文发布于:2023-11-25 02:06:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项卡   开新   Javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!