Never been to DZone Snippets before?

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

About this user

Doc Aha

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

logging bookmarklets for debugging javascript

Dragg the following two links to your browser's toolbar (tested with Firefox 1.5 and IE 6) :
49.cl
49.sh

When you are writing some javascript embedded in a webpage and you want to debug your code, this gives you way to log things without alerting them.

Example :
   1  
   2  // part of embedded javascript code
   3  log49 = [];//log49 is global variable.
   4  var num = 1;
   5  log49.push('num = '+num); // log the state of num
   6  num = num+1;
   7  log49.push('second num ='+num); // again.


Then the two bookmarklets let you reset or view the log in runtime.
49.cl lets you reset the log by clearing the list stored in the global variable log49.
49.sh shows you the log by joining and alerting the value of log49.

Open Text Links // Bookmarklet

Open Text Links : this bookmarklet collects strings of the form http://... or https://... in the user-selected text and then open them all in new windows. tested with Firefox 1.5 and IE 6.

The code
   1  
   2  javascript:(function(){var w=window,d=document,rg=/\bhtt(p|ps)\:\/\/\S+\b/ig,gS='getSelection';var links=(''+(w[gS]? w[gS]():(d[gS]?d[gS]():d.selection.createRange().text))).match(rg);if(confirm(links.join('\n')))for(var i=0,len=links.length;i<len;i++) w.open(links[i]);alert('done');})()


Code with comments
   1  
   2  javascript:(function(){
   3    var w=window,d=document,rg=/\bhtt(p|ps)\:\/\/\S+\b/ig,gS='getSelection';
   4  
   5    // set links = the list of strings of the form http:/... or https:/... in the selected area
   6    var links=(''+(w[gS]? w[gS]():(d[gS]?d[gS]():d.selection.createRange().text))).match(rg);
   7  
   8    // if user confirms the list then open them all in new windows and then alert 'done'.
   9    if(confirm(links.join('\n')))
  10       for(var i=0,len=links.length;i<len;i++)
  11          w.open(links[i]);alert('done');
  12  })()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS