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

jquery - Refresh currently open tab with Javascript when a new tab is opened - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question asked Nov 10, 2016 at 14:33 dead pimpdead pimp 231 gold badge1 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

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

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

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.

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

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

Yes it is with document.body method.

Declare your window:

w = window.open("yourUrl", "MYtest", "width=700, height=500");

And in your function you can access with document.body method:

html = "I Append my new html";
$(w.document.body).html(html);

You need something like this (jquery code inside) :

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Title</title>
  <script src="https://code.jquery./jquery-1.12.4.min.js"></script>
</head>
<body>
    <a id="myLink" href="http://www.google.">Click me</a>
    <div id="toUpdate">Not updated</div>
</body>

<script>
$(function() {
    $("#myLink").on("click", function(event) {
        event.preventDefault();
        window.open(event.target.href, '_blank');
        $("#toUpdate").text("Go on google");
    }).on("mousedown", function(event) {
        event.preventDefault();
        if (event.which === 2)
            $("#toUpdate").text("Go on google");
    });
});
</script>
</html>

Edited to add Firefox support

发布评论

评论列表(0)

  1. 暂无评论