views:

4529

answers:

3

Why does this not work in ff/chrome?

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

(used as a bookmarklet)

+1  A: 

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

Matthew Flaschen
+3  A: 

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>
bdonlan
i am trying to force a save-as on a pdf file. i don't think I can change the headers
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.
Andrej
Indeed you can, and here's an example of just that :)
bdonlan
wow, that's an awesome idea. thanks!do you know if there is something i could do for forcing save as on pdf files not on my server?
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.
bdonlan
ok, thanks bdonlan. never would've thought of the htaccess file.
A: 

Firefox doesn't support execCommand. In fact it seems to be IE-only.

lc
is there a similar firefox function?
not that I know of, you'll want to use the content-disposition header as bdonlan suggests.
lc