试用小书签;重定向正常?

时间:2015-06-13 21:35:17

标签: javascript html5 bookmarklet

我试图在制作bookmarklet的过程中变得更好。它们如此迷人!但是,在控制台中完美运行的代码并不像bookmarklet一样工作,因为在执行之后,页面被重定向到代码的url。有原因吗?

代码:

<a href='javascript:try{document.getElementsByTagName("title")[0].innerHTML=prompt("new title")||"empty"}catch(e){}'>Change Title</a>

JavaScript的:

try {
    document.getElementsByTagName("title")[0].innerHTML = prompt("new title")||"empty" // in the event that there is no user input
} catch(e) {} // in the event that no title is found

1 个答案:

答案 0 :(得分:2)

尝试在您的bookmarklet链接末尾附加;void(0)

this other answer中有关于bookmarklet的必要性,这是一个很好的解释。

此处还提供了有关bookmarklet返回值的更多信息,http://subsimple.com/bookmarklets/rules.php#ReturnValues