Skip to content

Instantly share code, notes, and snippets.

@Wuodan
Forked from MrSwitch/saveAs.js
Last active December 18, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wuodan/5718018 to your computer and use it in GitHub Desktop.
Save Wuodan/5718018 to your computer and use it in GitHub Desktop.
// window.saveAs
// Shims the saveAs method, using saveBlob in IE10.
// And for when Chrome and FireFox get round to implementing saveAs we have their vendor prefixes ready.
// But otherwise this creates a object URL resource and opens it on an anchor tag which contains the "download" attribute (Chrome)
// ... or opens it in a new tab (FireFox)
// @author Andrew Dodson
// @copyright MIT, BSD. Free to clone, modify and distribute for commercial and personal use.
window.saveAs || ( window.saveAs = (window.navigator.msSaveBlob ? function(b,n){ return window.navigator.msSaveBlob(b,n); } : false) || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs || (function(){
// URL's
window.URL || (window.URL = window.webkitURL);
if(!window.URL){
return false;
}
return function(blob,name){
var url = URL.createObjectURL(blob);
// Test for download link support
if( "download" in document.createElement('a') ){
var a = document.createElement('a');
a.setAttribute('href', url);
a.setAttribute('download', name);
// Create Click event
var clickEvent = document.createEvent ("MouseEvent");
clickEvent.initMouseEvent ("click", true, true, window, 0,
clickEvent.screenX, clickEvent.screenY, clickEvent.clientX, clickEvent.clientY,
clickEvent.ctrlKey, clickEvent.altKey, clickEvent.shiftKey, clickEvent.metaKey,
0, null);
// dispatch click event to simulate download
a.dispatchEvent (clickEvent);
}
else{
// fallover, open resource in new tab.
window.open(url, '_blank', '');
}
};
})() );
@Wuodan
Copy link
Author

Wuodan commented Jun 5, 2013

replaced all "event." with "clickEvent."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment