Javascript Debut - From Chrome Console To A Bookmarklet
I am new to JS and am trying to write a simple bookmarklet for a website that has divs with class 'collapseable' set to display: none; I want by one click to set all elements with
Solution 1:
When I tried making a bookmarklet myself, I had the same problem. My solution was to use void (function({ /* your code */ })()
.
javascript:void(function(){
var getElsByCN = document.getElementsByClassName ||
function(classname, node) {
if(!node) node = document.body;
var a = [],
re = newRegExp('\\b' + classname + '\\b'),
els = node.getElementsByTagName("*");
for (var i = 0, j = els.length; i < j; i++)
if (re.test(els[i].className))
a.push(els[i]);
return a;
};
var elems = getElsByCN('collapseable');
for (var i = 0; i < elems.length; i++)
elems[i].style.display = 'block';
)();
This works because it returns an undefined
value due to the use of void
. When your script wrote block
on the page, it was likely because that was the last value set/accessed by the funtion.
Post a Comment for "Javascript Debut - From Chrome Console To A Bookmarklet"