如何在 Javascript 中发送 HTTP 请求而不打开窗口?
现在我正在尝试使用 window.open
打开一系列 URL,
for(i=0;i<10;i++)
{
window.open("myapp://arg1/arg2/arg3/number_"+i);
}
除了弹出十个窗口之外,它工作正常。我想知道是否有任何方法可以在未显示的窗口中完成此工作?帮助将不胜感激!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只想发送这些请求并且不关心他们的响应,您可以创建一个图像:
AFAIR,我尝试过的所有浏览器(没有测试 IE)都下载了资源并且没有'不需要我实际显示图像或将其附加到 DOM 或其他任何内容。这对于记录和预取内容很有用,但除此之外就没有什么用了。
If all you want to do is send those requests and you don't care about their response, you could create an Image:
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.
不,一点也不。您在循环中调用
window.open()
10 次。您可能需要一个 JavaScript 模式窗口。
No. Not at all. You are calling
window.open()
10 times in a loop.You may want a JavaScript modal window.
有没有办法在不显示新窗口的情况下做到这一点?
当然:
不过,说真的,您需要解释一下您期望发生什么 -
window.open
的目的是打开新的(弹出)窗口,如果这不是您想要的,那么您需要另一个功能。更新:阅读您的评论后,您似乎想要做的是向该页面发送 HTTP 请求(而不是在新窗口中打开该页面)。执行此操作的正常方法是使用 XMLHttpRequest 对象发送请求。
如果您使用 JavaScript 框架/库(例如 jQuery),您可能会发现该对象被包装在更易于使用的函数中。
Is there a way to do this without new windows being shown?
Sure:
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.