Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open vCard with Javascript

With a QR code vcard, the user scans the code with their phone and then the dialog with the "add to contacts" pops up on their phone, such as the code below:

enter image description here

How can I do the same but instead of a QR code scan, I want it to do the same with a button click.

I have tried the following:

var btn = document.getElementById(“clickMe”);
btn.addEventListener(“click”, loadvcard);
function loadvcard(){
 url = "BEGIN%3AVCARD%0AVERSION%3A3.0%0AN%3ADoe%3BJohn%0AFN%3AJohn%20Doe%0ATITLE%3A08002221111%0AORG%3AStackflowover%0AEMAIL%3BTYPE%3DINTERNET%3Ajohndoe%40gmail.com%0AEND%3AVCARD";
 window.open(url);

}
like image 201
Bruno Avatar asked May 21 '26 01:05

Bruno


1 Answers

You can open your vcard in the browser as a data url if you want.

Your code would be:

var btn = document.getElementById(“clickMe”);
btn.addEventListener(“click”, loadvcard);
function loadvcard(){
 var data = "BEGIN%3AVCARD%0AVERSION%3A3.0%0AN%3ADoe%3BJohn%0AFN%3AJohn%20Doe%0ATITLE%3A08002221111%0AORG%3AStackflowover%0AEMAIL%3BTYPE%3DINTERNET%3Ajohndoe%40gmail.com%0AEND%3AVCARD";
 window.open("data:text/x-vcard;urlencoded," + data);
}
like image 170
Max1Truc Avatar answered May 22 '26 15:05

Max1Truc