A very minor improvement of the code by Awesomeness01 (no need for anchor tag) with addition as suggested by trueimage (support for IE):
// Function to download data to a file function download(data, filename, type) { var file = new Blob([data], {type: type}); if (window.navigator.msSaveOrOpenBlob) // IE10+ window.navigator.msSaveOrOpenBlob(file, filename); else { // Others var a = document.createElement("a"), url = URL.createObjectURL(file); a.href = url; a.download = filename; document.body.appendChild(a); a.click(); setTimeout(function() { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 0); } }
Tested to be working properly in Chrome, FireFox and IE10.
In Safari, the data gets opened in a new tab and one would have to manually save this file.
Related Posts:
- How to use Javascript to read local text file and read line by line?
- How do I include a JavaScript file in another JavaScript file?
- How To Save Canvas As An Image With canvas.toDataURL()?
- “Cross origin requests are only supported for HTTP.” error when loading a local file
- “Cross origin requests are only supported for HTTP.” error when loading a local file
- Can I use complex HTML with Twitter Bootstrap’s Tooltip?
- How do I redirect to another webpage?
- Invariant Violation: Objects are not valid as a React child
- How to get all place details from a Place Details Request using Google Places API Web Service?
- How can I merge properties of two JavaScript objects dynamically?
- Uncaught SyntaxError: Invalid or unexpected token
- JavaScript TypeError: Cannot read property ‘style’ of null
- Invalid shorthand property initializer
- nodejs – error self signed certificate in certificate chain
- How do I loop through or enumerate a JavaScript object?
- Getting Unexpected Token Export
- How to use iMonkey in an iOS app
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
- Mouseover & Mouseout w/ Javascript
- Format JavaScript date as yyyy-mm-dd
- Programmatically change the src of an img tag
- Unable to preventDefault inside passive event listener
- Angular JS Uncaught Error: [$injector:modulerr]
- Nested objects in javascript, best practices
- Scroll Automatically to the Bottom of the Page
- npm WARN … requires a peer of … but none is installed. You must install peer dependencies yourself
- How do I check if string contains substring?
- What does “export default” do in JSX?
- Cannot find module ‘internal/fs’ after upgrading to node 7
- OnChange event handler for radio button (INPUT type=”radio”) doesn’t work as one value
- InvalidValueError: not an instance of HTMLInputElement
- How to clear react-native cache?
- Execute PHP function with onclick
- blocked a frame of origin “null” from accessing a cross-origin frame – chrome
- How can I resolve the error “URL scheme must be “http” or “https” for CORS request.” for this code [duplicate]
- Initializing an Array with a Single Value
- Is it possible to stop JavaScript execution?
- Remove commas from the string using JavaScript
- How can you remove all documents from a collection with Mongoose?
- TypeError: ‘undefined’ is not an object
- Uncaught TypeError: Cannot read property ‘appendChild’ of null
- javascript setTimeout() not working
- How to open a URL in a new Tab using JavaScript or jQuery?
- How to sort an object array by date property?
- React router not showing browser history
- How to define DO NOTHING in JavaScript
- How to get the difference between two arrays in JavaScript?
- What happened to Lodash _.pluck?
- Simplest/cleanest way to implement a singleton in JavaScript
- Extract hostname name from string
- How to connect signalR from angularJs
- JavaScript button onclick not working
- What counts as CPU Intensive tasks (eg. sorting, searching etc?)
- HTML make text clickable without making it a hyperlink
- Uncaught ReferenceError: React is not defined
- How to get the browser viewport dimensions?
- Getting HTML form values
- How to fix javascript .toFixed is not a Function error
- jQuery checkbox checked state changed event
- Javascript, viewing [object HTMLInputElement]
- Why onbeforeunload event is not firing
- etect the Enter key in a text input field
- Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (
) - What’s the equivalent of Java’s Thread.sleep() in JavaScript?
- Any advantage of using wp_scripts and is_IE when enqueuing scripts
- What do the args for Gutenberg subpackage “hooks” function “doAction” mean?
- Include Javascript as Plain (No file inclusion)
- Adding wp_ajax to a theme outside functions.php (on specific template page)
- Insert shortcode in post editor from javascript (Visual / HTML)
- Provide specific example for block variation
- Block editor: How to check if block editor has initialized and populated the data store?
- Contact Form 7 – Give each checkbox a class?
- How to add a custom.js file for a child-theme in a swift way?
- is_page not working when loading javascript with add_action in functions.php
- Custom wp.editor.initialize settings ignored
- Why my javascript code not working for the dashboard of wordpress admin?
- Detect When User Reads Full Article? [closed]
- How to set callback to execute after all the tinyMCE editors have been initialized?
- How to add classes and events to image in javascript using Gutenberg?
- useSelect second parameter
- Adding Code into Theme Customizer Header
- Javascript in wordpress
- Do I just put the html in a page when enqueueng or do I also have to reference js file from the html page [closed]
- How to inherit custom javascript from parent to child pages?
- jQuery UI Tools: Scrollable: More than 1 instance possible?
- imporing HTML+javascript from REST API
- how to add a custom javascript file to our theme
- Why does my array sort order changes when I pass it to JS using WP_REST_Response?
- WordPress Guternberg Content For React Native
- Custom block update rendering when reused
- How do I add my own scripts to the add_action(‘wp_enqueue_scripts’) function?
- Post title and url not showing on google map marker [closed]
- Including js script at end of header
- contact form 7 javascript and wpcf7_before_send_mail action
- Use one javascript variable into another javascript file
- Embed Kajabi into WP
- Google AdSense Ad not showing up in WordPress Blog [closed]
- How to load JavaScript modules with script_loader_tag hook?
- How to securely set dynamic HTML content with JavaScript?
- Astra Mobile Cart Drawer is blank on home page