e dot dot dot
a mostly about the Internet blog by

home << Programming << Javascript << Universal JS Download Modal Triggering Code

October 2025
Sun Mon Tue Wed Thu Fri Sat
     
 

Wed, 09 Apr 2025


Universal JS Download Modal Triggering Code

This handy javascript programming takes two incoming arguments, and invokes the download dialogue modal for any text supplied to it. Just provide it with a fileName (to be used when saving the "file"), and fileContents, and see for yourself.

Highlight and copy the Javascript code below, starting with the "function" line, and ending with the very last right-squiggly-bracket that's followed by a semi-colon.

#########################################################
JS Programming Below
#########################################################
function createAndDownloadFile(fileName, fileContent) {

// Create a Blob from the content (assuming it's text data)
const blob = new Blob([fileContent], { type: 'text/plain' });

// Create an object URL for the Blob
const url = URL.createObjectURL(blob);

// Create an anchor element
const a = document.createElement('a');

// Set the download attribute with the specified file name
a.download = fileName;

// Set the href attribute to the Blob URL
a.href = url;

// Append the anchor element to the document body (required for Firefox)
document.body.appendChild(a);

// Trigger a click on the anchor element to start the download
a.click();

// Clean up by removing the anchor element
document.body.removeChild(a);

// Optionally, release the object URL after use to free memory
URL.revokeObjectURL(url);
};
#########################################################
JS Programming Above
#########################################################


posted at: 9:54am on 09-Apr-2025
path: /Programming/Javascript | permalink


0 writeback(s)

comment...

 
Name:
URL/Email: (optional)
[http://... or mailto:you@wherever]
Title: (optional)
Comments:
Please enter the anti-spam code shown below: 

home << Programming << Javascript << Universal JS Download Modal Triggering Code

October 2025
Sun Mon Tue Wed Thu Fri Sat
     
 


Categories
 - blog home

 - Announcements  (0)
 - Annoyances  (0)
 - Career_Advice  (0)
 - Domains  (0)
 - Downloads  (3)
 - Ecommerce  (0)
 - Fitness  (0)
 - Home_and_Garden  (0)
     - Cooking  (0)
     - Tools  (0)
 - Humor  (0)
 - Notices  (0)
 - Observations  (1)
 - Oddities  (2)
 - Online_Marketing  (0)
     - Affiliates  (1)
     - Merchants  (1)
 - Programming  (0)
     - Bookmarklets  (1)
     - Browsers  (1)
     - DHTML  (0)
     - Javascript  (4)
     - PHP  (0)
     - PayPal  (1)
     - Perl  (37)
          - blosxom  (0)
     - Unidata_Universe  (22)
 - Random_Advice  (1)
 - Reading  (0)
     - Books  (0)
     - Ebooks  (0)
     - Magazines  (0)
     - Online_Articles  (3)
 - Resume_or_CV  (1)
 - Reviews  (2)
 - Rhode_Island_USA  (0)
     - Providence  (1)
 - Shop  (0)
 - Sports  (0)
     - Football  (0)
          - Cowboys  (0)
          - Patriots  (0)
     - Futbol  (0)
          - The_Rest  (0)
          - USA  (0)
 - Technology  (4068)
 - Windows  (1)
 - Woodworking  (0)


Archives
 -2025  October  (144)
 -2025  September  (157)
 -2025  August  (169)
 -2025  July  (161)
 -2025  June  (155)
 -2025  May  (157)
 -2025  April  (149)
 -2025  March  (164)
 -2025  February  (151)
 -2025  January  (167)
 -2024  December  (155)
 -2024  November  (156)
 -2024  October  (158)
 -2024  September  (155)


My Sites

 - Millennium3Publishing.com

 - SponsorWorks.net

 - ListBug.com

 - TextEx.net

 - FindAdsHere.com

 - VisitLater.com