Javascript - 书签中的转义字符问题
我有一段很好的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会尝试将整个代码块包装在一个函数中,以便您的书签(未转义)看起来像这样:
但这只是我。更重要的是,当您将其转换为小书签形式时,这些转义字符需要进行两次转义。
Date 实例上还有一个名为“getFullYear”的函数。
I would try wrapping that whole chunk of code in a function, so that your bookmarklet (un-escaped) looks like this:
But that's just me. More important, those escaped characters would need to be double-escaped when you're turning it into bookmarklet form.
Also there's a function on Date instances called "getFullYear".
为什么 %20 - 我的小书签都不需要它
Why the %20 - none of my bookmarklets ever needed that
另一种方法是让脚本进行转义:
Another way is to get the script to do the escaping: