Experimenting with bookmarklet; redirecting normal?

72 views Asked by At

I'm trying to become better in the art of making bookmarklets. They're so fascinating! However, the code that works perfectly in the console doesn't work as bookmarklet, as, after the execution, the page is redirected to the url of the code.; is there a reason?

Code:

<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

There are 1 answers

1
Kurt Ruppel On BEST ANSWER

Try appending ;void(0) at the end of your bookmarklet link.

There's a good explanation of why that's necessary for bookmarklets in this other answer.

More info on bookmarklet return values is also provided here, http://subsimple.com/bookmarklets/rules.php#ReturnValues .