简体   繁体   English

Mailto:作为变量的主题

[英]Mailto: subject as a variable

** EDIT: Thanks, Shivan Raptor! **编辑:谢谢,Shivan Raptor! I kinda forgot about "encodeURIcomponent()"! 我有点忘了“encodeURIcomponent()”! ** **

For some reason, I'm making a Javascript bookmarklet that quickly emails one of my two emails. 出于某种原因,我正在制作一个Javascript书签,可以快速通过电子邮件发送我的两封电子邮件中的一封。 (I'll just show the indented JS to make it easier, as I can convert it.) It uses a couple variables for the subject and body, which are inputted by prompts. (我只是展示缩进的JS以使其更容易,因为我可以转换它。)它使用几个变量作为主题和正文,由提示输入。

var address = confirm("School email?");

        var sub = prompt("What is the subject?");

        var bod = prompt("What would you like the message to read?");

        if(address == true) {
            window.location.assign("mailto:areiter@hightechhigh.org?Subject=sub&body=bod");
            alert("Message sent to 'areiter@hightechhigh.org'.");
        }
        else {
            window.location.assign("mailto:burningphantom13@gmail.com?Subject=sub&body=bod");
            alert("Message sent to 'areiter@hightechhigh.org'.");
        }

The only problem is that the variables "sub" and "bod" are what the subject and body appear as, respectively. 唯一的问题是变量“sub”和“bod”分别是主体和身体所显示的。 So the code opens up Outlook, and has an email ready to be sent with the subject "sub" and "bod" in the body. 所以代码打开了Outlook,并且有一封电子邮件准备好与主体中的“sub”和“bod”一起发送。 Is there a way for the subject and body of a Javascript code to be the values of variables? 有没有办法让Javascript代码的主题和正文成为变量的值? Remember, it's going to be a bookmark, so there can't be any HTML. 请记住,它将是一个书签,因此不能有任何HTML。

You can replace : 你可以替换:

window.location.assign("mailto:areiter@hightechhigh.org?Subject=sub&body=bod");

with: 有:

window.location.assign("mailto:areiter@hightechhigh.org?Subject=" + encodeURIComponent(sub) + "&body=" + encodeURIComponent(bod));

so that the variable sub and bod will be dynamic according to user input. 这样变量subbod将根据用户输入动态变化。 encodeURIComponent encodes the parameters, so if the user inputs special characters, the codes will not crash. encodeURIComponent对参数进行编码,因此如果用户输入特殊字符,则代码不会崩溃。

Change code as following, 更改代码如下,

    var address = confirm("School email?");

    var sub = prompt("What is the subject?");

    var bod = prompt("What would you like the message to read?");

    if(address == true) {
        window.location.assign("mailto:areiter@hightechhigh.org?Subject=" + sub + "&body=" + bod);
        alert("Message sent to 'areiter@hightechhigh.org'.");
    }
    else {
        window.location.assign("mailto:burningphantom13@gmail.com?Subject=" + sub + "&body=" + bod);
        alert("Message sent to 'areiter@hightechhigh.org'.");
    }

Hope this will work :) 希望这会工作:)

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

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