i love hidden iframes

2007-09-14 @ 01:53#

recently, 've redicovered the value of hidden iframes in HTML documents. in a project i'm wrapping up at work, i use them as target values for file downloads. that means i can have a target page that is a server script to handle the details of streaming a download file without actually navigating the browser away from the host page. another key advantage of using iframes as navigation targets is that you can return client script that pops a standard alert box that contains status or other information.

here's an example:

<!-- the links using iframe as a target -->
<a href="file.html" target="hidden_iframe">file.zip</a>

<!-- the hidden iframe -->
<iframe name="hidden_iframe" id="hidden_iframe" 
   style="display:none;" scrolling="no" frameborder="0"></iframe>

<!-- sample file.html document to do the real downloading -->
<script type="text/javascript">
    location.href="http://someserver.com/file.zip";
</script>

tonight, i modified a page that contained a handful of download links from my s3 account to actually point to a local HTML file with a single javascript line to download the file from s3. why? because, with this arrangement, my server logs now contain an entry for every file download. not bad, eh?

an added bonus to this approach is that, since the actual download requires javascript, robots will not navigate to the download page and execute the download. so i save some bandwidth, too!

code