Javascript - 书签中的转义字符问题

发布于 2024-08-29 22:18:31 字数 799 浏览 5 评论 0原文

我有一段很好的 javascript,

<script type="text/javascript"><!--
var d = new Date();
d.setTime(d.getTime()-86400*1000);
window.location = "https://mail.google.com/mail/?shva=1#search/in%3Ainbox+before%3A"+(d.getYear()+1900)+"%2F"+(d.getMonth()+1)+"%2F"+d.getDate();
//-->
</script>

当我访问存储 javascript 的 .html 文件时,它的工作效果完全令人满意。

但是,我希望通过使用小书签获得相同的效果 - 但是,当我将其放入

javascript:var%20d%20=%20new%20Date();%20d.setTime(d.getTime()-86400*1000);%20window.location%20=%20"https://mail.google.com/mail/?shva=1#search/in%3Ainbox+before%3A"+(d.getYear()+1900)+"%2F"+(d.getMonth()+1)+"%2F"+d.getDate();

我的书签我被带到 re:2010/4/20 而不是 re%3A2010%2F4%2F20

我假设书签系统或 javascript 中的转义字符存在一些问题,但我没有得到任何结果 - 有人愿意伸出援手吗?

I've got a nice piece of javascript

<script type="text/javascript"><!--
var d = new Date();
d.setTime(d.getTime()-86400*1000);
window.location = "https://mail.google.com/mail/?shva=1#search/in%3Ainbox+before%3A"+(d.getYear()+1900)+"%2F"+(d.getMonth()+1)+"%2F"+d.getDate();
//-->
</script>

That works entirely satisfactorily when I visit the .html file that i store the javascript in.

However, I'd like to get the same effect from using a bookmarklet - however, when I put

javascript:var%20d%20=%20new%20Date();%20d.setTime(d.getTime()-86400*1000);%20window.location%20=%20"https://mail.google.com/mail/?shva=1#search/in%3Ainbox+before%3A"+(d.getYear()+1900)+"%2F"+(d.getMonth()+1)+"%2F"+d.getDate();

into my bookmark I'm taken to re:2010/4/20 rather than
re%3A2010%2F4%2F20

I'm assuming that there's some problem with the escape characters in either the bookmarking system or javascript but I'm not getting anywhere - anyone care to lend a hand?

Joe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初相遇 2024-09-05 22:18:31

我会尝试将整个代码块包装在一个函数中,以便您的书签(未转义)看起来像这样:

(function() {
    var d = new Date();
    d.setTime(d.getTime()-86400*1000);
    window.location = 
      "https://mail.google.com/mail/?shva=1#search/in%3Ainbox+before%3A" + 
      d.getFullYear()+"%2F"+(d.getMonth()+1) + "%2F" + d.getDate();
})();

但这只是我。更重要的是,当您将其转换为小书签形式时,这些转义字符需要进行两次转义。

javascript:%28function%28%29%20%7B%0A%20%20%20%20%20%20%20%20var%20d%20%3D%20new%20Date%28%29%3B%0A%20%20%20%20%20%20%20%20d.setTime%28d.getTime%28%29-86400*1000%29%3B%0A%20%20%20%20%20%20%20%20window.location%20%3D%20%0A%20%20%20%20%20%20%20%20%20%20%22https%3A//mail.google.com/mail/%3Fshva%3D1%23search/in%253Ainbox+before%253A%22%20+%20%0A%20%20%20%20%20%20%20%20%20%20d.getFullYear%28%29+%22%252F%22+%28d.getMonth%28%29+1%29%20+%20%22%252F%22%20+%20d.getDate%28%29%3B%0A%20%20%20%20%7D%29%28%29%3B

Date 实例上还有一个名为“getFullYear”的函数。

I would try wrapping that whole chunk of code in a function, so that your bookmarklet (un-escaped) looks like this:

(function() {
    var d = new Date();
    d.setTime(d.getTime()-86400*1000);
    window.location = 
      "https://mail.google.com/mail/?shva=1#search/in%3Ainbox+before%3A" + 
      d.getFullYear()+"%2F"+(d.getMonth()+1) + "%2F" + d.getDate();
})();

But that's just me. More important, those escaped characters would need to be double-escaped when you're turning it into bookmarklet form.

javascript:%28function%28%29%20%7B%0A%20%20%20%20%20%20%20%20var%20d%20%3D%20new%20Date%28%29%3B%0A%20%20%20%20%20%20%20%20d.setTime%28d.getTime%28%29-86400*1000%29%3B%0A%20%20%20%20%20%20%20%20window.location%20%3D%20%0A%20%20%20%20%20%20%20%20%20%20%22https%3A//mail.google.com/mail/%3Fshva%3D1%23search/in%253Ainbox+before%253A%22%20+%20%0A%20%20%20%20%20%20%20%20%20%20d.getFullYear%28%29+%22%252F%22+%28d.getMonth%28%29+1%29%20+%20%22%252F%22%20+%20d.getDate%28%29%3B%0A%20%20%20%20%7D%29%28%29%3B

Also there's a function on Date instances called "getFullYear".

找个人就嫁了吧 2024-09-05 22:18:31

为什么 %20 - 我的小书签都不需要它

Why the %20 - none of my bookmarklets ever needed that

浅忆 2024-09-05 22:18:31

另一种方法是让脚本进行转义:

window.location = "https://mail.google.com/mail/?shva=1#search/" + encodeURIComponent("in:inbox") + "+" + encodeURIComponent("before:" + d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate());

Another way is to get the script to do the escaping:

window.location = "https://mail.google.com/mail/?shva=1#search/" + encodeURIComponent("in:inbox") + "+" + encodeURIComponent("before:" + d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文