简体   繁体   中英

How to insert href URL to Javascript var?

I am trying to create a script for autoreply in google apps and would love to have that reply to have some formatting. I've been getting a syntax error due to the quotes closing with the opening of the href tag. Is there a way to insert the URL into the html message? My scribbles below.

 function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var message = "We are out of the office until Monday morning and will reply to your email then. If you are having an emergency, please text (800) 590-2508, we'll do our best to reply as soon as possible."; var htmlMessage = "<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>"; var date = new Date(); var day = date.getDay(); var hour = date.getHours(); if ([6,0].indexOf(day) > -1) { var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval; var threads = GmailApp.search('is:inbox after:' + timeFrom); for (var i = 0; i < threads.length; i++) { if (threads[i].isUnread()){ threads[i].reply(message, {htmlBody: htmlMessage}); threads[i].markRead(); threads[i].star(); } } } }

The error is on line 4

 var htmlMessage = "<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>";

The quotes after htmlMessage = open the message, and the quotes after the first href= close it, so the rest of the message breaks the syntax.

Any insight on how to include the whole html under the var statement?

Thanks!

add \\ before "

 var htmlMessage = "<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href=\\"https://sampleurl.com\\" target=\\"_blank\\" rel=\\"noopener\\">Link text</a></p><p><a href=\\"https://anothersammple.com\\" target=\\"_blank\\" rel=\\"noopener\\">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>";

another method is to replace " with ' inside the string

Replace " from the beginning and end with tild `

Check the documentation Here

var htmlMessage = `<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>`;

 function autoReply() { var interval = 5; // if the script runs every 5 minutes; change otherwise var message = "We are out of the office until Monday morning and will reply to your email then. If you are having an emergency, please text (800) 590-2508, we'll do our best to reply as soon as possible."; var htmlMessage = `<p>Hi,</p><p>Thanks for your message.</p><p>We are out of the office until Monday morning and will reply to your email then. In the meantime, check out some helpful resources:</p><p><a href="https://sampleurl.com" target="_blank" rel="noopener">Link text</a></p><p><a href="https://anothersammple.com" target="_blank" rel="noopener">Link text</a></p><p>If you are having an emergency, please text (800) 000-0000, we'll do our best to get back to you as soon as possible.</p><p>Have a pleasant weekend,</p><p>My Signature</p>`; document.write(htmlMessage) var date = new Date(); var day = date.getDay(); var hour = date.getHours(); if ([6,0].indexOf(day) > -1) { var timeFrom = Math.floor(date.valueOf()/1000) - 60 * interval; var threads = GmailApp.search('is:inbox after:' + timeFrom); for (var i = 0; i < threads.length; i++) { if (threads[i].isUnread()){ threads[i].reply(message, {htmlBody: htmlMessage}); threads[i].markRead(); threads[i].star(); } } } } autoReply()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM