Open two windows onClick button javascript [duplic

2019-08-21 13:48发布

This question already has an answer here:

I am trying to open two different windows with the click of a button, but when i click the button it opens the same window twice. This is my function:

function flush(form) {
    var custid = form.custid.value;
    var url1 = "https://www.blabla.com?type=customer&id="+custid;
    flushWindow1 = window.open(url1);
    var url2 = "https://web.blabla.com?type=customer&id="+custid;
    flushWindow2 = window.open(url2);
}  

Does anyone know how I can do this?

3条回答
叼着烟拽天下
2楼-- · 2019-08-21 14:25

try open in new window/tab by add parameter '_blank'
and then focus the window you want by place window.focus(); behind window you want to focus after open two new window

function flush(form) {
    var custid = form.custid.value;
    var url1 = "https://www.blabla.com?type=customer&id="+custid;
    flushWindow1 = window.open(url1, '_blank');
    var url2 = "https://web.blabla.com?type=customer&id="+custid;
    flushWindow2 = window.open(url2, '_blank');
    window.focus(); // focus on url2
}  
查看更多
趁早两清
3楼-- · 2019-08-21 14:25

Try providing second argument to window.open() method, which is the name of the window, if name is different then same url will open in different windows. e.g.

function flush(form) {
    var custid = form.custid.value;
    var url1 = "https://www.blabla.com?type=customer&id="+custid;
    flushWindow1 = window.open(url1,"flushWindow1");
    var url2 = "https://web.blabla.com?type=customer&id="+custid;
    flushWindow2 = window.open(url2,"flushWindow2");   
}

Demo

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-21 14:36

From : JSfiddle Two windows on a Click

$('document').ready(function(){
    $('input').click(function(){
        window.open("https://www.google.com.pk/search?q=123&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
        window.open("https://www.google.com.pk/search?q=abc&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a")
    });
});  
查看更多
登录 后发表回答