Thursday, May 5, 2011

Does execCommand SaveAs work in firefox?

Why does this not work in ff/chrome?

javascript: document.execCommand('SaveAs','true','http://www.google.com');

(used as a bookmarklet)

From stackoverflow
  • As Microsoft puts it, "There is no public standard that applies to this method."

  • execCommand is not completely standardized across browsers. Indeed, execCommand('SaveAs', ...) only seems to be supported on IE. The recommended way to force a save-as would be to use a content-disposition: attachment header, as described in http://www.jtricks.com/bits/content_disposition.html

    Since this is part of the HTTP header, you can use it on any file type. If you're using apache, you can add headers using the .htaccess file, as described here. For example:

    <FilesMatch "\.pdf$">
    <IfModule mod_headers.c>
    Header set Content-Disposition "attachment"
    # for older browsers
    Header set Content-Type "application/octet-stream"
    </IfModule>
    </FilesMatch>
    
    Andrej : I think content disposition is part of the HTTP header, not part of the document, so you should be able to use it for pdf files.
    bdonlan : Indeed you can, and here's an example of just that :)
    bdonlan : Copy them to your server? :)
    bdonlan : But more seriously, I don't know. Try opening up another question about that specific topic, maybe someone else will.
  • Firefox doesn't support execCommand. In fact it seems to be IE-only.

    lc : not that I know of, you'll want to use the content-disposition header as bdonlan suggests.

0 comments:

Post a Comment