Kill Browser Downloads With Javascript

Monday, August 8, 2011


JavaScript is one of the tools in your Web developer toolbox you can use to ensure your visitors have a positive experience on your website. Occasionally, you may have a Web page where you or your visitor may want to stop downloading images or other files, particularly if those files are large or the server hosting the files is slow. You can use JavaScript to call the browser's "stop" function to kill downloads yet leave your JavaScript running and functional.

Instructions



Open your Web page in your favorite text or HTML editor. Switch to HTML code view, if necessary.


Add the following function to the script section of the page.

function killDownload () {
if (window.stop !== undefined) {
window.stop() ;
}
else if (document.execCommand !== undefined) {
document.execCommand("Stop", false) ;
}
}

Some browsers support the "window.stop()" method, and others support stopping downloads using the "document.execCommand("Stop", false)" method. The function tests for support, and calls the appropriate method. If neither method is supported, nothing will happen.

Call your "killDownload()" function wherever you need to stop ongoing downloads. You can call it from your own JavaScript functions, or provide a link your visitors can click. For example, you might provide your visitors the following link in the body of your Web page:


Kill the Download!


Test and review the page to make sure it functions correctly. Make sure to perform any necessary cleanup after stopping the downloads. For example, you may want to give your visitor some extra feedback by using an alert window to display a message, or redirecting the page to an error page.


Read more: http://goo.gl/OC9H3
  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comments: