How To Prevent Bookmarklet From Loading Its Result?
I have a simple bookmarklet for presenting in a prompt() the value of a meta element. It works, but after running the window loads either just the string 'null' or the prompted val
Solution 1:
The accepted way these days is to wrap your code in an "immediately invoked function expression" like this
(function(){ /*your code */})();
This is better than adding void(0);
at the end because wrapping your code in a function prevents conflicts between named variable and function in your bookmarklet and in those in the scope of the parent HTML document.
In other words, if you run your bookmarklet as it is now on a web page that uses JavaScript code with an already existing variable "description", you could create problems.
Solution 2:
Add void(0);
at the end of it.
javascript:var%20description;var%20metas=document.getElementsByTagName('meta');for(var%20x=0,y=metas.length;x<y;x++){if(metas[x].name.toLowerCase()=="description"){description=metas[x];}}prompt("Meta%20Description",description.content);void(0);
Post a Comment for "How To Prevent Bookmarklet From Loading Its Result?"