Generating Unique Strings in Javascript

Here is a simple way to generate a unique string in javascript.

    var ts = (new Date()).getTime().toString();
    var url = "ajaxHandler.php?param1=value1&paramn=valuen&t=" + ts;
    //do ajax call...

This method is very helpful for ajax developers, to overcome the problem of cached ajax requests in IE.

Update:
Here’s another way of creating unique strings

String.unique = String.guid = String.uid = String.uuid = function(){
    var idx = [], itoh = '0123456789ABCDEF'.split('');

    // Array of digits in UUID (32 digits + 4 dashes)
    for (var i = 0; i < 36; i++) { idx[i] = 0xf & Math.random() * 0x10; }
    // Conform to RFC 4122, section 4.4
    idx[14] = 4; // version
    idx[19] = (idx[19] & 0x3) | 0x8; // high bits of clock sequence

    // Convert to hex chars
    for (var i = 0; i < 36; i++) { idx[i] = itoh[idx[i]]; }

    // Insert dashes
    idx[8] = idx[13] = idx[18] = idx[23] = '-';

    return idx.join('');
}

Sample Use:

   var id=String.guid();
   va u = String.uuid();
Advertisements

TextArea SelectedText, SelectionStart, SelectionEnd, SelStart, SelEnd (IE and FireFox)

Simple way of getting the html textarea’s selection properties selectedText, selectionStart, selectionEnd:

Javascript:

 function getTextAreaSelection() {
   var textArea = document.getElementById('textarea1');
   if (document.selection) { //IE
       var bm = document.selection.createRange().getBookmark();
       var sel = textArea.createTextRange();
       sel.moveToBookmark(bm);
     
       var sleft = textArea.createTextRange();
       sleft.collapse(true);
       sleft.setEndPoint("EndToStart", sel);
       textArea.selectionStart = sleft.text.length
       textArea.selectionEnd = sleft.text.length + sel.text.length;
       textArea.selectedText = sel.text;  
   }
   else if (textArea.selectionStart){ //FF
      textArea.selectedText = textArea.substring(textArea.selectionStart,textArea.selectionEnd);
   }
  
   alert("Selection Start==> " + textArea.selectionStart + "\n" +
      "Selection End  ==> " + textArea.selectionEnd + "\n" +
      "Selected Text  ==> " + textArea.selectedText + "\n" +
      "TextArea Value ==> " + textArea.value);
 }

HTML:

<textarea id="textarea1"></textarea> <button onclick="getTextAreaSelection()">Get Selection Info</button>

Generating Unique Strings in NET

Approach 1:

String s1 = Guid.NewGuid().ToString();
String s2 = Guid.NewGuid().Tostring("N");
String s3 = Guid.NewGuid().ToString("P");
String s3 = Guid.NewGuid().ToString("D");


Approach 2:

String s1 = System.IO.Path.GetRandomeFileName();
String s2 = System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(),null);

Comparison or the 2 Approaches:
1. Approach1 generates at least 32 characters (0-9 and a-f) letters only.
Hint: larger size on the database, absolutely unique.
2. Approach2 Generates at least 8 characters (0-9 and a-z) letters.
Hint: Good for Captcha, less size on the database,  might have duplicates

Reversing Flash Timeline

Just recently, I have encountered a really simple problem that I have hard time figuring out the solution, but in hours of looking for that solution I just found one.

In the web, if we search for reversing a timeline in flash, we will find mostly solutions that are done using another movie clip, but It doesn’t solve my problem. I only need to reverse the timeline without creating another movie clip.

Here’s how I figure it out. The solution that worked for me, is to use the setInterval and the clearInterval.

To do so, create a simple animation/tween, then on the last frame, insert the following action script:

stop();
var speed:Number = 40;
function goto(destination) {
    if (_root._currentframe == destination)
       clearInterval(nIntervals);
    else
       prevFrame();
}
var nIntervals = setInterval(goto, speed,15);

In this, example I assumed you have more than 15 frames in your animation, let say you have 60-frame animation, From frame 60 the timeline will reverse play until frame 15.

Flash Disk Virus Cleaner

I just found this tool in the web and find it useful, save it with .bat extension then execute. It helps a lot.

@ECHO OFF
cls
echo Zabyer Flash Disk Virus Cleaner v1.0.3
echo http://www.zabyer.org
echo Last Updated: October 14, 2007  8:06am
echo ---------------------------------
echo Files that will be deleted:
echo imgkulot, INFO.exe, Desktop.ini, scvhosts.exe
echo TTM*.vbs, krag.exe, sysdll.exe, RavMon.exe, msv*.dll
echo and other flash disk pest!!!
echo ---------------------------------
echo Resetting Stupid Virus attributes...
echo (Disarming flash disk pest!)
attrib -r -h -s -a autorun.*
attrib -r -h -s -a TTM*.*
attrib -r -h -s -a imgkulot*.*
attrib -r -h -s -a RECYCLER\INFO.exe
attrib -r -h -s -a RECYCLER\Desktop.ini
attrib -r -h -s -a sysdll.exe
attrib -r -h -s -a krag.exe
attrib -r -h -s -a RavMon*.*
attrib -r -h -s -a msv*.dll
attrib -r -h -s -a scvhosts.exe
attrib -r -h -s -a svhost.exe
attrib -r -h -s -a C:\Windows\svhost.exe
attrib -r -h -s -a C:\Windows\svhost32.exe
attrib -r -h -s -a "New Folder".exe
echo ---------------------------------
echo Attributes Reset!
echo Preparing Clean-Up Procedures...
echo Next step is to delete all the pest.
echo ---------------------------------
pause
echo ---------------------------------
echo Deleting Stupid Virus...
echo ---------------------------------
del autorun.*
del TTM*.*
del imgkulot*.*
del RECYCLER\INFO.exe
del RECYCLER\Desktop.ini
del sysdll.exe
del krag.exe
del RavMon*.*
del msv*.dll
del scvhosts.exe
del svhost.exe
del C:\Windows\svhost.exe
del C:\Windows\svhost32.exe
del "New Folder".exe
echo ---------------------------------
echo Flash Disk Cleaned-Up!
echo ---------------------------------
echo Please report for new virus so that this Virus Cleaner will be updated. 🙂
echo Send to admin@zabyer.org or camilord@zabyer.org you feedbacks...
echo ---------------------------------
pause

Step-by-step Creation of Executable Jar for Java

1. Start Command Prompt. 
2. Navigate to the folder that holds your class files: 
   C:\>cd\
   C:\>cd java-code
3. Set path to include JDK’s bin.
	For example:
		C:\java-code> SET PATH=%PATH%:c:\Program Files\Java\jdk1.5.0\bin
4. Compile your class(es): 
		C:\java-code> javac TestForm.java
		C:\java-code> javac Main.java
5. Create a manifest file: 
	C:\java-code> echo Main-Class: Main >manifest.txt
6. Create a jar file: 
	C:\java-code> jar cvfm welcome.jar manifest.txt Main.class TestForm.class
7. Test your welcome.jar: 
	C:\java-code> welcome.jar

And that’s it we’re done.