简体   繁体   中英

How to send HTTP requests without opening up windows in Javascript?

Now I'm trying to open a series of URLs with window.open

for(i=0;i<10;i++)
{
    window.open("myapp://arg1/arg2/arg3/number_"+i);
}

It works fine except popping up ten windows.And I wonder if there's any way to do this work with windows not shown? Help would be appreciated!

If all you want to do is send those requests and you don't care about their response, you could create an Image:

for(i=0;i<10;i++)
{
    new Image().src = "myapp://arg1/arg2/arg3/number_"+i;
}

AFAIR, all the browsers I've tried that in (didn't test IE) downloaded the resource and didn't require me to actually display the image or append it to the DOM or anything. This is good for logging and prefetching stuff, but not much else.

No. Not at all. You are calling window.open() 10 times in a loop.

You may want a JavaScript modal window.

Is there a way to do this without new windows being shown?

Sure:

for(i=0;i<10;i++)
{
    // window.open("myapp://arg1/arg2/arg3/number_"+i);
}

Seriously though, you need to explain what it is that you expect to happen - the purpose of window.open is to open new (popup) windows, and if thats not what you want then you need another function.

Update: Having read your comment it appears that what you wish to do is send a HTTP request to that page (rather than open that page in a new window). The normal way to do this is to use the XMLHttpRequest object to send the request.

If you are using a JavaScript framework / library (such as jQuery) you will probably find that this object is wrapped in easier-to-use functions.

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