DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Bookmarklet Builder
Write readable JavaScript code and get your bookmarklet easily.
sample.html:
<div id="src" name="Bookmarklet Name - Show Selection">
alert ( "Selection: "+(
// FF
document.getSelection
? document.getSelection()
// IE
: ((document.selection && document.selection.createRange)
? document.selection.createRange().text
: "unknown browser")
))
</div>
<script src="bookmarklet-builder.js">
</script>
bookmarklet-builder.js:
function build(js) {
return "javascript:"+escape(
js
.replace(/\/\/.*\n/g, "")
.replace(/\s+/g, " ")
.replace(/\s*([=()\{\}\[\]\,]|$|^)\s*/g, "$1"))
}
var src = document.getElementById("src");
document.write("<a href='"+build(src.textContent || src.innerText)+"'>"+src.getAttribute("name")+"</a>");
// NOTE: in IE comments not handled
1. Put bookmarklet code into div with id=src
2. set name= text inside the link that will be displayed
3. include bookmarklet-builder.js AFTER div
4. open the html
5. test it by clicking the link
6. bookmark the link





