简体   繁体   English

在新窗口中打开 URL 的 HTML 按钮

[英]HTML button to open URL in a new window

I'm populating an email template and I'm having some issues with the button onclick event.我正在填充电子邮件模板,但按钮 onclick 事件出现了一些问题。

What I am trying to achieve is when clicking on the button it should open a URL in a new window rather than a new tab.我想要实现的是当单击按钮时,它应该在新窗口而不是新选项卡中打开一个 URL。

Currently when the email gets sent it is losing the onclick event functionality.目前,当电子邮件被发送时,它正在失去 onclick 事件功能。 Any recommendations on how can I achieve this?关于如何实现这一目标的任何建议?

I have also tried creating the button in the html template rather than replacing the tags as code below, but still run into the same issue.我还尝试在 html 模板中创建按钮,而不是将标签替换为下面的代码,但仍然遇到相同的问题。

HTML Code: HTML代码:

    <p>
        Hi,
    </p>
    <p>
        A [JobType], to start on [JobStartDate]
        <br />
        Click here to view the job: [JobUrl]
    </p>
    <p>
        <b>Job Details:</b>
        <br />
        - Reference: [JobReferenceNumber]
        <br />
        - Checklist: [AnswerEvaluationName]
        <br />
        - Created by: [JobCreator]
        <br />
    </p>
    <p>
        Kind regards<br />
    </p>
    <p class="auto-style6">
        Please do not reply to this system generated email
    </p>
</body>
public void CreateEmailNotificationForNewJob()
{
   // Populate the Email Template
   string EmailRecipientTestingNotes = "";
   string EmailSubject = "";
   string EmailTemplate = EmailSender.LoadEmailTemplate("EmailNewJob.html");
   string EmailTo = "";
   string TestButton = ("<input type=\"button\" onclick=\"window.open('https://www.google.com/','mywin','width=500,height=500');\" >");

   EmailSubject = "Job/task ";
   EmailTemplate = EmailTemplate.Replace("[JobReferenceNumber]", "Reference");
   EmailTemplate = EmailTemplate.Replace("[AnswerEvaluationName]", "Daily Update");
   EmailTemplate = EmailTemplate.Replace("[JobStartDate]", "Today");
   EmailTemplate = EmailTemplate.Replace("[JobType]", "1");
   EmailTemplate = EmailTemplate.Replace("[CompanySiteName]", "");
   EmailTemplate = EmailTemplate.Replace("[SiteName]", "");
   EmailTemplate = EmailTemplate.Replace("[JobUrl]", TestButton);

   EmailTo = "test@test.co.za";

   try
   {
      // Send email code
   }
   catch (Exception ex)
   {
      // Add handling
   }
}

Most email clients don't allow execution of JavaScript like a browser would (usually this is for security reasons).大多数电子邮件客户端不允许像浏览器那样执行 JavaScript(通常这是出于安全原因)。

But if you want to create a link to a URL to display in the email, you can just use a hyperlink instead of a button.但是,如果您想创建指向要在电子邮件中显示的 URL 的链接,您可以只使用超链接而不是按钮。 (You can of course always use CSS to make it look like a button, if you wish.) (当然,如果您愿意,您当然可以始终使用 CSS 使其看起来像一个按钮。)

string TestButton = ("<a href='https://www.google.com/'>Click here</a>");

Email clients won't accept JavaScript.电子邮件客户端不接受 JavaScript。 The best you can do is to use a standard link with the target attribute set to _blank:您能做的最好的事情是使用目标属性设置为 _blank 的标准链接:

string TestButton = ("<a href='www.blah.com' target='_blank'>link text</a>");

Having said that, most email clients will open in a new/blank tab anyway.话虽如此,大多数电子邮件客户端无论如何都会在新/空白选项卡中打开。 However, this is helpful still for those who use click "view this email in your browser" (on Outlook desktop, for example, this is generated automatically).但是,这对于使用单击“在浏览器中查看此电子邮件”的用户仍然很有帮助(例如,在 Outlook 桌面上,这是自动生成的)。

Just use a hyperlink instead of a button because email clients won't accept JavaScript.只需使用超链接而不是按钮,因为电子邮件客户端不接受 JavaScript。 Of course you can use css to make it looks like a button.当然你可以使用css让它看起来像一个按钮。

Button:按钮:

string TestButton = ("<a class='button' href='https://www.google.com/'>Click here</a>");

Css: css:

.button {
font: bold 11px Arial;
text-decoration: none;
background-color: #EEEEEE;
color: #333333;
padding: 2px 6px 2px 6px;
border-top: 1px solid #CCCCCC;
border-right: 1px solid #333333;
border-bottom: 1px solid #333333;
border-left: 1px solid #CCCCCC;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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