Monday, January 28, 2013

Basic Syntax conversion 2


1. Change class name   

document.all("detailChoices").className = "Hidden"  =>      $("#detailChoices").attr("class", "Hidden"); 

2. Make ReadOnly

frmPayment.MethodOfPayment.readonly = True  =>         $('#MethodOfPayment').attr('readonly', true);

3. Error Handling


VBScript error-handling requires two elements that work together.
You can turn it on with the On Error Resume Next statement and turn it off with On Error GoTo 0.

ex:-

On Error Resume Next
     window.location = “/reports/test.asp”
On Error Go To 0


try {
     window.location = “/reports/test.asp”;
} catch (err) {
}

Thursday, January 10, 2013

Handle Ajax Requests


Handling ajax request with jquery is a easy task. if you have a xml file with id= "Data" have to send as xml document.

XML doucment,
        <xml id="Data"> </xml>

Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
oXMLHTTP.open "POST", "/xml/loadZip.asp", False
oXMLHTTP.send Data.XMLDocument

If oXMLHTTP.statusText = "OK" Then
Set oResponse = oXMLHTTP.responseXML.documentElement
Set oStatus = oResponse.selectSingleNode("status")
End If

when you are converting, you can assigne xml document to parameter or can use it directly.In following example I used xml document directly.
  • oXMLHTTP.statusText = "OK" means success: in javascript
    $.ajax({
type: "POST",
async: false,
dataType: "xml",
data: <xml id="Data"> </xml>,
url: "/xml/loadZip.asp",
success: function(data) {
            var oStatus = $(data).find("status").text();
        }
    });

Wednesday, January 9, 2013

Convert VB script loops to Javascript


1. Convert vb script for loop to javascript

        For nI = 0 To 3
            window.alert("Number : " & nI )
Next

Upper bound is setting to 3 in above loop. when converting, javascript condition should be nI <= 3, (not nI < 3)

        for(var nI = 0; nI <= 3; nI++) {
            alert("Number : " + nI );
        }

out put,

    Number : 0
    Number : 1
    Number : 2
    Number : 3


Thursday, January 3, 2013

VB Script to Javascript Conversions(Basic)

Converting basic language syntaxs,

1. UCase()  => toUpperCase()

ex:-
UCase(string) => string.toUpperCase();


2. InStr() => indexOf()

ex:-
inStr(string1, string2) => string1.indexOf("string2");

InStr([start,]string,searchvalue[,compare]) => string.indexOf(searchvalue,start);



3. Mid() => substr()

ex:-
Mid(string, 2, 3) => string.substr(2-1,3);

4. Replace() => .replace()

ex:-
Replace(string, "textToReplace", "newText") => string.replace("textToReplace", "newText");

5. CStr() => String()


ex:-
CStr(expressionToString) => String(expressionToString);


6. cInt() => Math.round()

ex:-
cInt(anyNumber) => Math.round(anyNumber);


7. CDbl() => parseFloat()

ex:-
CDbl(134.345) => parseFloat(134.345);


8.object.Exists(keyvalue) -> $.inArray(value, array)

    The Exists method is used to determine whether a key already exists in the specified Dictionary. Returns True if it does and False otherwise.

Code: 
<%
dim mdsTeam
set mdsTeam=CreateObject("Scripting.Dictionary")
mdsTeam.Add "k", "Kushan"
mdsTeam.Add "b", "Bandu"
mdsTeam.Add "m", "Madura"
mdsTeam.Add "v", "Vijitha"
If
guitars.Exists("v") Then
   guitars.Item("v") = "Thilina"
End If
%> 


Output: 
"Kushan"
"Bandu"
"Madura"
"Thilina"
 

javascript ex:-
solutions
jQuery has a utility function for this.
$.inArray(value, array)
   Returns index of value in array. Returns -1 if array does not contain value.
9.
MsgBox() => confirm()
ex:-
If MsgBox("Are you sure you want to delete ") <> vbYes Then
   Validate = False
End If
if(!confirm("Are you sure you want to delete ?")) {
   return false;
}
10. Round(decimalNumber,decimalPoints) => decimalNumber.toFixed(decimalPoints);
ex:-
Round(2.123,2) => 2.123.toFixed(2);




Javascript equivalent for Do While loop in vbscript


VB script -
1. The do while statatement repeat a block of code until condition is true,


Do While nI>10
nI = nI + 1
Loop


If nI equals 9, above code will not be excuted,

2. This block of condition excuted at least once

Do 
nI = nI + 1
Loop While nI>10

Even nI is less than 10, the block of code is excuted once.

Javascript -


1.This block of code is equalant to above first vbscript example


while (nI>10)
  {
  nI = nI ++;
  }

2.This block of code is equalant to above second vbscript example

do
  {
nI = nI ++;
  }
while (nI>10);

This code also excuted once whether condition is failed or not