Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create .ics file with javascript or jquery

Tags:

I am trying to create an .ics file when a user clicks a button.

So far the code I have is

msgData1 = $('.start-time').text(); msgData2 = $('.end-time').text(); msgData3 = $('.Location').text();  var icsMSG = "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Our Company//NONSGML v1.0//EN\nBEGIN:VEVENT\nUID:[email protected]\nDTSTAMP:20120315T170000Z\nATTENDEE;CN=My Self ;RSVP=TRUE:MAILTO:[email protected]\nORGANIZER;CN=Me:MAILTO::[email protected]\nDTSTART:" + msgData1 +"\nDTEND:" + msgData2 +"\nLOCATION:" + msgData3 + "\nSUMMARY:Our Meeting Office\nEND:VEVENT\nEND:VCALENDAR";  $('.button').click(function(){     window.open( "data:text/calendar;charset=utf8," + escape(icsMSG)); }); 

This downloads a .ics file but when I try to open this in iCal I am told it can not read the file.

like image 501
joshuahornby10 Avatar asked Oct 02 '13 12:10

joshuahornby10


People also ask

How create ICS file in jquery?

click(function(){ window. open( "data:text/calendar;charset=utf8," + escape(icsMSG)); });

What is the difference between ICS and iCal?

ics format, also known as iCalendar, is a common bond between these and many other calendar applications, and no conversion is required. Calendar sharing through iCalendar is built in to the current versions of both Outlook and iCal.


2 Answers

I used the ics.js solution mentioned by InsanelyADHD.

One problem with the solution was that Chrome did not detect the filetype correctly and tried to open the file as text with the editor.

I changed the download function to open the text simply with:

window.open( "data:text/calendar;charset=utf8," + escape(calendar)); 

My fork on Github is icsFormatter.js

Regarding the license: I have contacted the original author to include a GPL - after that I will include one too.

like image 123
smartbart24 Avatar answered Sep 22 '22 13:09

smartbart24


There is an open source project for this:

  • ics.js

It's 100% JavaScript and worked on all modern browsers except for IE 9 and below.

If you are interested in how they get the files working, you can check their source. They use the following libraries to perform the heavy lifting:

  • FileSaver.js
  • Blob.js
like image 28
InsanelyADHD Avatar answered Sep 19 '22 13:09

InsanelyADHD