简体   繁体   中英

Open the same window multiple times using JavaScript

I'm new to JavaScript and trying to learn. How can I open the same window multiple times using JavaScript? Also, it didn't work when I changed the function name.

Here is the function:

<script type='text/javascript'>
function window(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250');");
}
</script>

Use a random number to open a popup as many times as you want:

function openPopUp (url) {
    var randomno = Math.floor((Math.random()*100)+1); 
    window.open(url,'PopUpWindow'+randomno,'scrollbars=1,menubar=0,resizable=1,width=850,height=500');
}

Try something like this:

var numWindows = 0;
var windows = new Array(100);
var parameters = "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250";

function openNewWindow(url) {
    numWindows += 1;
    var title = "Window #"+numWindows;
    windows[numWindows] = window.open(url, title, parameters);
}

To access the windows, do this:

windows[num]

where num is the id of the window. The first id is 1, the second is 2, and so on.

Try omitting with id parameter, like so:

<script type='text/javascript'>
    function window(URL) {
        window.open(URL,'','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left=650,top=250');
    }
</script>

The 2nd parameter of window.open() (here it's the timestamp) must be different, otherwise window.open() will load the new window inside the existing(opened before with the same name ->that's what the 2nd parameter assigns to the window).

You can also use "_blank" as 2nd parameter for open()

First, I would strongly encourage you to abandon using eval. It's very unsecure and not good to use.

Second, you can accomplish this without eval like so:

function openWindow(URL) {
    day = new Date();
    id = day.getTime();
    windowCol[id] = window.open(URL, id, 
        'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250');
}

And just to avoid confusion, even though "window" works as a function name, I'd suggest changing it to make the code more readable as it may get confused with the global window object.

Try this.

<script type='text/javascript'>
function window(URL) {
window.open(URL, "_blank", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=800,left = 650,top = 250');
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM