将事件添加到谷歌日历
将日历事件添加到交互时,用户将此事件添加到其常规日历应用程序中通常很重要。现有功能允许用户下载一个标准ICS文件到他们的设备上,并将该事件导入到他们的日历中,例如MS Outlook或Apple calendar。
虽然这也适用于谷歌日历,这将更容易为那些用户,如果“添加到日历”按钮,只需打开他们的谷歌日历与一个新的事件,包含相关的详细信息。这似乎是一个很好的机会,使用一些自定义javascript来覆盖'添加到日历'按钮的默认行为
下面的javascript可以添加到你内网的开发者框架Masterpage javascript中——一旦保存了“添加到日历”按钮,就会简单地打开谷歌日历选项卡,而不是下载ics文件!简单!
$(document).ready(function() {if (window.location.href.indexOf('Calendar/ event .aspx') > -1){//日历事件页面所以添加谷歌日历按钮//从DOM中解析日期var dates = $("#body_lblDates").text();Var from = dates.split('-')[0].trim();Var to = dates.split('-')[1].trim();var fromDate = new Date(Date.parse(from));if (isNaN(date .parse(from))) {// from date无效,所以今天或明天fromDate = new date ();if (from.toLowerCase().indexOf('today') == -1){//明天fromDate.setDate(fromDate.getDate() + 1);} var fromHour = from。分割(' ')[1].split[0](“:”);var fromMinutes = from。分割(' ')[1].split(“:”)[1]; if (from.indexOf('PM') > -1) { // it's PM so add 12 hours to end time fromHour = parseInt(fromHour) + 12; } fromDate.setHours(fromHour); fromDate.setMinutes(fromMinutes); } // use the same as from date for an initial set (covers all day events or events where only the end time is displayed) var toDate = new Date(fromDate); if (!isNaN(Date.parse(to))) { // If the to date can be parsed then use it toDate = new Date(Date.parse(to)); } else if (to.indexOf(':') > -1) { // If the to date can't be parsed it's either an all day event or just an end time with no date part var toHour = to.split(' ')[0].split(':')[0]; var toMinutes = to.split(' ')[0].split(':')[1]; if (to.indexOf('PM') > -1) { // it's PM so add 12 hours to end time toHour = parseInt(toHour) + 12; } toDate.setHours(toHour); toDate.setMinutes(toMinutes); } // build the date and time strings as required for google querystring var fromFormattedDate = ""; var toFormattedDate = ""; fromFormattedDate = fromFormattedDate.concat(fromDate.getFullYear(), ("0" + (fromDate.getMonth() + 1)).slice(-2), ("0" + fromDate.getDate()).slice(-2)); toFormattedDate = toFormattedDate.concat(toDate.getFullYear(), ("0" + (toDate.getMonth() + 1)).slice(-2), ("0" + toDate.getDate()).slice(-2)); if (to.toLowerCase() != "all day event") { // add the time fromFormattedDate = fromFormattedDate.concat("T", fromDate.toLocaleTimeString().replace(/[^0-9]/g, "")); toFormattedDate = toFormattedDate.concat("T", toDate.toLocaleTimeString().replace(/[^0-9]/g, "")); } var dates = "".concat(fromFormattedDate, "/", toFormattedDate); var text = $("#body_pageTitle_divLeft").text().trim(); var details = $("#body_lblEventDescription").text().trim().replace(/\n/g, "
"); var location = $("#body_lblLocation").text().trim(); var url = "https://www.google.com/calendar/event?action=TEMPLATE&dates="; url = url.concat(dates, "&text=", text, "&details=", details, "&location=", location, "&rp=false&ctz=Europe/London"); // update the attributes on the element including the relevant accessibility attributes $("#body_btnGetInvite").prop("href", url); $("#body_btnGetInvite").prop("target", "_blank"); $("#body_btnGetInvite").prop("title", "Add to google calendar"); $("#body_btnGetInvite").click(function() { if ($("#divAttendanceButtons .btn").length > 0 && $("#divAttendanceButtons .btn-success").length == 0) { alert('Please remember to RSVP!'); return false; } }); } });
更新 3个月前