gpt4 book ai didi

javascript - 如何在小书签 JavaScript 中输入换行符

转载 作者:行者123 更新时间:2023-12-03 01:18:21 24 4
gpt4 key购买 nike

我尝试编写一个创建电子邮件的书签,但存在使用换行符的问题。

当我从 Chrome 控制台执行以下代码时,它工作正常。但是,从书签执行时此代码不起作用。我检查了代码,原因似乎是 var body1 包含换行符(%0D%0A)。

有人知道如何在书签 JS 中插入换行符吗?

javascript:(function(){
var today = new Date();
var url = 'https://mail.google.com/mail/?view=cm';
var to = 'emailfrombookmarklet@example.com';
var subject = '【weather%20report】【' + today.getFullYear() + '-' + ("00" + (today.getMonth() + 1)).slice(-2) + '-' + ("00" + today.getDate()).slice(-2) + '%20bar】%20email%20from%20bookmarklet';
var body1 = 'Hi%2C%0D%0A%0D%0AThis is Kim Kardashian%2E%0D%0AIt%27s%20sunny%20today%2E%0D%0A%0D%0Adate%3A';
var targetDate = '%20' + today.getFullYear() + '-' + ("00" + (today.getMonth() + 1)).slice(-2) + '-' + ("00" + today.getDate()).slice(-2);
var body2 = '%0D%0AName%3A%20Kim%20Kardashian%0D%0A';
var body3 = '%0D%0ABest%20Regards%2C';
url += '&to=' + to + '&su=' + subject + '&body=' + body1 + targetDate + body2 + body3;
window.open(url);})();

最佳答案

最好完全摆脱使用编码字符:

javascript:(function() {
var today = new Date();
var url = 'https://mail.google.com/mail/?view=cm';
var to = 'emailfrombookmarklet@example.com';
var subject = '【weather report】【' + today.getFullYear() + '-' + ('00' + (today.getMonth() + 1)).slice(-2) + '-' + ('00' + today.getDate()).slice(-2) + ' bar】 email from bookmarklet';
var body1 = 'Hi,\n\nThis is Kim Kardashian.\nIt\'s sunny today.\n\ndate:';
var targetDate = ' ' + today.getFullYear() + '-' + ('00' + (today.getMonth() + 1)).slice(-2) + '-' + ('00' + today.getDate()).slice(-2);
var body2 = '\nName: Kim Kardashian\n';
var body3 = '\nBest Regards,';
url += '&to=' + encodeURIComponent(to) + '&su=' + encodeURIComponent(subject) + '&body=' + encodeURIComponent(body1 + targetDate + body2 + body3);
window.open(url);
})();

关于javascript - 如何在小书签 JavaScript 中输入换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51889864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com