简体   繁体   English

如何在不打开Javascript窗口的情况下发送HTTP请求?

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

Now I'm trying to open a series of URLs with window.open 现在我正在尝试使用window.open打开一系列URL

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: 如果您只想发送这些请求并且不关心他们的响应,那么您可以创建一个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. AFAIR,我尝试过的所有浏览器(没有测试IE)下载了资源,并没有要求我实际显示图像或将其附加到DOM或任何东西。 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. 你在循环中调用window.open() 10次​​。

You may want a JavaScript modal window. 您可能需要一个JavaScript模式窗口。

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. 但是说真的,你需要解释一下你期望发生什么 - window.open的目的是打开新的(弹出窗口)窗口,如果那不是你想要的那么你就需要另外一个功能。

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). 更新:阅读您的评论后,您希望做的是向该页面发送HTTP请求(而不是在新窗口中打开该页面)。 The normal way to do this is to use the XMLHttpRequest object to send the request. 执行此操作的常规方法是使用XMLHttpRequest对象发送请求。

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. 如果您使用的是JavaScript框架/库(例如jQuery),您可能会发现此对象包含在更易于使用的函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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