2010-04-21 17 views
1

我有一個很好的一段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(); 

到我的書籤我心有所屬重新:2010年4月20日,而不是 re%3A2010%2F4%2F20

我假設在書籤系統或javascript中的轉義字符存在一些問題,但我沒有得到任何地方 - 任何人都在關心伸出援手嗎?

回答

1

我會嘗試在包裝函數的代碼塊整體,讓你的書籤(非轉義)看起來是這樣的:

(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 

另外還有一個名爲「getFullYear」的Date實例的函數。

+0

工作得很好,非常感謝:) – Joe 2010-04-30 08:26:14

1

爲什麼20% - 沒有我的書籤工具的不斷需要的

0

另一種方法是讓腳本做轉義:

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