How to Use JavaScript to Download


If you want to avoid using the "meta-refresh" HTML tag to trigger a download from your Web page, you can instead use a snippet of JavaScript code to automatically download a file instead. This snippet of code will wait until the page is loaded using the "window.onLoad" function, followed by the "document.location" function to download the file. Information can be displayed on the Web page about the download before the download dialog box appears.

  • Open your Web page document in a text editor or HTML editing application.

  • Click to place your cursor on a line between the "<head>" and "</head>" tags, near the top of the document.

  • Type "<script type="text/javascript">" and press the "Enter" key. This is an opening JavaScript tag that tells the Web browser to interpret the next lines as JavaScript, until the closing JavaScript tag is encountered.

  • Type "window.onload = function(){" and press "Enter." This tag instructs the Web browser to load the rest of the page before loading the next function.

  • Type "document.location = '';" into the next line, but replace "" with the URL, if needed, and file name that will be downloaded automatically. Press "Enter."

  • Type "}" and then press "Enter." This curly bracket closes the JavaScript functions.

  • Type "</script>" to close the JavaScript tag.

  • Click "File" and "Save" to save the Web page document.

Related Searches


Promoted By Zergnet



You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!