Created
October 14, 2014 18:12
-
-
Save jasongabler/94c093d154c46c01dbf9 to your computer and use it in GitHub Desktop.
Grabs vCard data from an HTML container "#vcard" and turns it into an downloading octect-stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function( $ ) { | |
$(function() { | |
$('.vcard-download').click(function() { | |
// Get the vcard data stored within the same result container | |
var vcard = $('#vcard'); | |
// Generate a filename that is the person's fullname with all whitespace converted to dashes. | |
var filename = vcard.data('displayname').replace(/\s+/, '-') + '.vcf'; | |
// Initiate the download of the data as a file | |
download(vcard.html(), filename, 'text/vcard'); | |
}); | |
}); | |
// Convert data into a downloadable file stream | |
var download = function(content, filename, contentType){ | |
if (!contentType) { | |
contentType = 'application/octet-stream'; | |
} | |
var a = document.createElement('a'); | |
var blob = new Blob([content], {'type':contentType}); | |
a.href = window.URL.createObjectURL(blob); | |
a.download = filename; | |
a.click(); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment