如何將一個html組成一個字符串的小書籤,然後使用window.location
和document.write()
的組合來顯示它?
var links = "<html><head><title>Suggested links</title></head><body>";
links += "<p><a href='http://example.com/'>Example Suggested Link</a></p>";
links += "</body></html>";
window.location = 'javascript:document.write("' + links + '");' ;
你必須要小心你的報價,以確保正確的字符串被顯示,但快速測試後,它似乎尊重後退按鈕...
下面是一個示例: http://jsbin.com/ebuko4 - 它爲我工作在Firefox,但我還沒有在其他地方測試了它......
代碼:
<html>
<head>
<title>JS Bin Sample bookmarklet</title>
</head>
<body>
<p><a href='javascript:(function(){ var l = "<html><head><title>Does this bookmarklet work?</title></head><body><p>"+ "<a href=\"http://example.com/\">Suggested link: Example.com</a></p>" + "<p><a href=\"http://example.net/\">Suggested link: Example.net</a></p>" + "</bo" + "dy></html>"; window.location = "javascript:document.write(%27" + l + "%27);"; })()'>Sample Bookmarklet - drag me to your toolbar</a></p>
</body>
</html>
通過網站重定向至少可以很好地工作,因爲該URL將成爲瀏覽器歷史記錄的一部分,易於導航回去。您只需要有人爲您提供一個靜態HTML + Javascript頁面。 – joeytwiddle 2010-12-20 09:25:25