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-8 of 8 total  RSS 

how to open links in current window, new window, background.

function open2(url, opt){
  if (opt == 0) // current window
    window.location = url;
  else if (opt == 1) // new window
    window.open(url);
  else if (opt == 2) // background window
    {window.open(url); self.focus();}
}

cross-browser way to get selection.

function getSel(){
var w=window,d=document,gS='getSelection';
return (''+(w[gS]?w[gS]():d[gS]?d[gS]():d.selection.createRange().text)).replace(/(^\s+|\s+$)/g,'');
}

Code Snippets Feature Request

I wonder how I can search for snippets in Code Snippets.
Searching Code Snippets thru Google, is it the only way?

// insert code here..

general javascript code example

// description of your code here
sample code.
to test online bookmarklet builders.

alert('too       much'); //comment1
alert/*comment2*/('another     much');    var   a   =   1;
if(a == 1)
   a = 2
else
   a = 1

links for debugging javascript

// description of your code here

Loggers
my simple logger
MochiKit logger

Online interpreters
Squarefree JS shell 1.4
MochiKit JS interactive interpreter
TryIt Editor

debugging bookmarklets
view source
linked scripts list

// insert code here..

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 :
// part of embedded javascript code
log49 = [];//log49 is global variable.
var num = 1;
log49.push('num = '+num); // log the state of num
num = num+1;
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.

a test post

// description of your code here
google
2

ok, link has size limit.

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
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
javascript:(function(){
  var w=window,d=document,rg=/\bhtt(p|ps)\:\/\/\S+\b/ig,gS='getSelection';

  // set links = the list of strings of the form http:/... or https:/... in the selected area
  var links=(''+(w[gS]? w[gS]():(d[gS]?d[gS]():d.selection.createRange().text))).match(rg);

  // if user confirms the list then open them all in new windows and then alert 'done'.
  if(confirm(links.join('\n')))
     for(var i=0,len=links.length;i<len;i++)
        w.open(links[i]);alert('done');
})()
« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS