问题
I'm using jsonp to do cross-domain comet requests, and the "loading" status is really annoying.
Is there any way to suppress this with javascript?
For those who are unfamiliar with jsonp, it basically injects a script tag, except in my case, I'm hanging the request on my server without returning the request until a later time.
During this time, browsers see my request as a "loading" state.
I am using this: http://code.google.com/p/jquery-jsonp/
Thanks in advance!
回答1:
As far as I know, there is no way to suppress the loading status using Javascript, regardless of why you have it.
However, there is at least one alternative for cross-domain COMET which wouldn't trigger the loading status in the first place. XMLHttpRequest doesn't set the loading status and, according to my tests, the CORS (Cross-Origin Resource Sharing) spec which allows cross-domain XHR is pretty well supported.
Basically, support is as follows: (According to a mix of browser documentation and my own tests for a project I'm working on)
Full support in:
- Gecko 1.9.1+ (Firefox 3.5, SeaMonkey 2.0, etc. Tested good Firefox 3.6.8 and SeaMonkey 2.0.7)
- WebKit (Safari 4+, Chrome 3+, etc.. Tested working on Safari 4 on OSX, Safari 5 on WinXP, Chrome 5.0.375.127 (Stable channel), Midori 0.2.7, the new Flock, Epiphany 2.30.2, luakit, and uzbl)
Untested, but should be fully supported:
- Fluid (WebKit-based MacOS alternative to Mozilla Prism and Chrome's "Create Application Shortcuts...")
Limited support in:
- Internet Explorer 8 (Microsoft implemented an XDomainRequest() object instead and, using security as an excuse, didn't implement the flag to pass credentials and cookies with the request)
- Sleipnir (Support is determined by which version of MSHTML it's embedding)
Notably unsupported:
- Opera (As of 11.01.1190, no support whatsoever)
- Camino (As of 2.0.5, still based on Gecko 1.9.0 (Firefox 3.0))
- Arora (As of 0.10.2, inherits WebKit's CORS API but has a bug that causes requests to fail)
- old, Mozilla-based Flock (Based on Gecko 1.9.0 (Firefox 3.0))
It's not a complete list, but it's every browser with userscript support I could find to test. I've already taken the time to cite my sources on the CORS wikipedia page if you want them.
The simplest solution I can think of would be to test for CORS and then fall back to JSONP so that people using a modern browser get a perfect experience and people using something older see the loading status as an unavoidable side-effect.
Here's the MDC page on how CORS works.
回答2:
If you begin your first request after the page has finished loading, you should be able to avoid the loading indicator.
$(function () {
setTimeout(function () {
$.jsonp(...)
}, 1000);
});
来源:https://stackoverflow.com/questions/3744488/jsonp-comet-hanging-request-causes-ugly-loading-status-on-browsers