如何将href URL插入Javascript变量?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将href URL插入Javascript变量?相关的知识,希望对你有一定的参考价值。

我正在尝试在Google Apps中创建用于自动回复的脚本,并希望该回复具有某种格式。由于引号随着href标签的打开而闭合,因此我一直在语法错误。有没有一种方法可以将URL插入html消息中?我的涂鸦在下面。

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();
      
    
  

错误在第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>";

htmlMessage =后的引号打开消息,而第一个href =后的引号将其关闭,因此消息的其余部分破坏了语法。

关于如何在var语句下包含整个html的任何见解?

谢谢!

答案
检查文档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()

以上是关于如何将href URL插入Javascript变量?的主要内容,如果未能解决你的问题,请参考以下文章

Javascript:如何将其他变量传递给promise .catch函数

将变量插入标题位置 PHP

如何在Javascript变量中存储脚本url的结果

PDF JavaScript:将变量的值插入表单字段

如何使用modalForm包将javascript整数变量作为主键传递到django url

如何在url中传递JavaScript变量