Open two windows onClick button javascript [duplic

2019-08-21 14:35发布

问题:

This question already has an answer here:

  • Multiple Windows using window.open() 4 answers

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?

回答1:

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")
    });
});  


回答2:

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:

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