开发者

how to call stop() without forcing an error in webkit browsers

After weeks of epic开发者_开发问答 javascript researching I finally invented what I think is very cool Object. I ran into a task that needed me to cancel the uploading of images. I originally thought:

image.src='' 

would suffice, but the browser still downloads the image.

So my cool solution involves placing an invisible iFrame into the body and appending an image tag into that, if I would like to stop downloading i simply call

iframe.contentWindow.window.stop(); // and something similiar for IE 8

I have my entire solution but calling stop() during the download gives this in chrome :

GET http://xx.xx.com.au/images/products/1.png undefined (undefined) and this in safari :

Failed to load resource

basically I'm asking if there is a try-catch statement for this problem

obviously I have tried try{ iframe.contentWindow.window.stop(); }catch(e){}

on request Im more then happy to share this code, but I warn you its much more complicated than it sounds.


I think you might be on the wrong track with this one. Fundamentally, you're not meant to be able to control the browser with Javascript, merely affect the rendering of your own page. Things like network connectivity are managed entirely by the browser behind the scenes, so anything you can do to modify that is likely to be a hint/hack to some extent.

I'd take a step back and think about the semantics of what you're trying to do, from the perspective of the webpage. How is it you're triggering an image upload? Depending on the format of this hint/request, the semantics of cancelling it are likely to be different.

Also, what's your motivation for cancelling - saving bandwidth? I have the feeling that if you say "upload this image" (e.g. submit a form), the fact that it may take several seconds is an implementation detail that you can't really access via Javascript. Besides, what if the user has a T1 line and the image has already completed uploading by the time you want to cancel?

Without knowing the specifics, yet thinking about the request-response protocol, the natural thing to do would be to send a message afterwards saying "actually, delete/disregard that image that I just uploaded".

I'm dubious about using Stop for this purpose because:

  1. It may cancel all communications on the page - it's like a static/global method in that it will play very badly with anything else on your page. This may include other client-facing elements that you've put there, or external things such as ads (less income for you) or web tracking (inaccurate and sporadic data for you). It's just bad design to kill everything when you actually only want to stop one specific thing.
  2. I'm not even sure if the browser needs to stop "background" tasks when Stop is called, as it's usually a browser chrome command to stop changing the current page. Depending on how the browser considers the image upload (and how it's triggered), it could interpret the Stop command to mean "don't change the page I'm on", and thus continue uploading the image.

The fact that the code is "more complicated than it sounds" is a warning as well - adding even more layers of complication is unlikely to be the best way to tackle a simple-sounding requirement. Take a step back and thing about the fundamentals of what you're trying to achieve, again, and how you can "legitimately" ask the browser to do so via Javascript/HTML.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜