开发者

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();
    i开发者_StackOverflowd = 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>


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.


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 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜