Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling VBScript from JavaScript

    jayakodiu@yahoo.com

    Calling VBScript from JavaScript

    VBS has several functions not available directly in JS; a method of creating and calling VBS from JS is shown in the snippet below:

    <html>
    <body>
    </body>
    <script language=javascript>
    var fn,k
    var ele=document.createElement("script")
    var nn=document.body.appendChild(ele)
    nn.setAttribute("language","vbs")
    nn.text="sub vfmt(x,n)"+"\n"+"fn=formatnumber(x,n)"+"\n"+"end sub"
    nn.text="sub vin(tt,zt)"+"\n"+"k=instr(tt,zt)"+"\n"+"end sub"
    nn.text="sub vsgn(tt)"+"\n"+"k=sgn(tt)"+"\n"+"end sub"

    vfmt(1/3,3)
    alert(fn)
    vin('asd123','1')
    alert(k)
    vsgn(-2)
    alert(k)
    </script>
    </html>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    IE only, naturally - pity that other web browsers such as Firefox and Opera do not have built-in support for VBScript.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts

    vb functions for javascript

    here are a lot of the vb methods i spoke of the other day.
    i haven't tested all of them extensively, but most are simple.
    feel free to customize.


    Provides these 55 vb functions::
    ["Abs", "Array$", "Asc", "Atn", "Chr", "Cos", "Exp", "Filter", "Fix", "Hex", "Instr", "Instrrev", "Int",
    "Join", "LBound", "Left", "Log", "Ltrim", "Mid", "Now", "Oct", "Right", "Rnd$", "Rtrim", "Sgn", "Sin",
    "Space", "Split", "Sqr", "StrComp", "StrReverse", "String$", "Tan", "Trim", "UBound", "Val",
    "cBool", "cDate", "cInt", "cLng", "cStr", "datePart", "dateParts", "dateSerial", "formatDateTime",
    "formatNumber", "isArray", "isDate", "isEmpty", "isMissing", "isNull", "isNumeric", "isObject", "lCase", "uCase"];




    javascript, 5kb:
    Code:
    var vb=(function(  blnPublishAsGlobals ){
    
      var vb={"Abs":"Math.abs(a)",
    	"Array$":"new Array(a)",
    	"Asc":"(''+a).charCodeAt(0)",
    	"Atn":"Math.atan(a)",
    	"Chr":"String.fromCharCode(a)",
    	"Cos":"Math.cos(a)",
    	"Exp":"Math.pow(a,b)",
    	"Fix":"(''+a).split(/\\./g)[0]",
    	"Hex":"a.toString(16)",
    	"Instr":"a.indexOf(b)",
    	"Instrrev":"a.lastIndexOf(b)",
    	"Int":"parseInt(a)",
    	"LBound":"return 0",
    	"Left":"(''+a).substring(0, b)",
    	"Log":"Math.log(a)",
    	"Ltrim":"var re = /\\s*((\\S+\\s*)*)/; return String(a).replace(re, \"$1\");",
    	"Mid":"(''+a).substr(b-1, arguments[2])",
    	"Now":"new Date",
    	"Oct":"a.toString(32)",
    	"Right":"(''+a).substring(a.length - b)",
    	"Rnd$":"cint(Math.random() * (a + 1))",
    
    	"Rtrim":"var re = /((\\s*\\S+)*)\\s*/;return String(a).replace(re, \"$1\")",
    	"Sgn":"if (a === 0) {return 0;}if(a > 0) {return 1;} else {return -1;}",
    	"Sin":"Math.sin(a)",
    	"Sqr":"Math.sqrt(a)",
    	"StrReverse":"String(a).split('').reverse().join('')",
    	"Tan":"Math.tan(a)",
    	"Trim":"String(a).replace(/^\s+|\s+$/g,'')",
    	"UBound":"a.length - 1",
    	"Val":"parseFloat(a)",
    	"cBool":"!!a",
    	"cInt":"parseInt(a)",
    	"cStr":"(''+a)",
    	"isArray":"a.constructor == Array",
    	"lCase":"a.toString().toLowerCase()",
    	"uCase":"a.toString().toUpperCase()",
    	dateSerial:"new Date(a,b,arguments[2])",
    
    	dateParts:"function dateParts(dt){if(!dt.getTime){dt=new Date(dt);}return{m:dt.getMonth()+1,yyyy:dt.getFullYear(),d:dt.getDate(),y:[0,31,28,31,30,31,30,31,31,30,31,30,31].slice(0,dt.getMonth()).reduce(F.sum)+dt.getDate(),w:dt.getDay(),h:dt.getHours(),n:dt.getMinutes(),s:dt.getSeconds()};}",
    	datePart:"dateParts(b)[a]",
    	isNull:"a==null",
    	isNumeric:"isFinite(a)",
    	isMissing:"a===undefined",
    	isEmpty:"a==null||a===''||(a instanceof Array && a.length===0)",
    	isDate:"d instanceof Date||!isNaN(new Date(a).getTime())",
    	isArray:"a instanceof Array",
    	isObject:"!(a instanceof Array) && typeof a==='object'",
    	
    
    	"Filter":"function Filter(r, value, include, compare) {if (typeof include == \"undefined\") {include = true;}var tr = [];if (compare) {if (include) {for (var z = 0; z < r.length; z++) {if (r[z].toLowerCase().indexOf(value.toLowerCase()) > -1) {tr[tr.length] = r[z];}}} else {for (var z = 0; z < r.length; z++) {if (r[z].toLowerCase().indexOf(value.toLowerCase()) == -1) {tr[tr.length] = r[z];}}}} else {if (include) {for (var z = 0; z < r.length; z++) {if (r[z].indexOf(value) > -1) {tr[tr.length] = r[z];}}} else {for (var z = 0; z < r.length; z++) {if (r[z].indexOf(value) == -1) {tr[tr.length] = r[z];}}}}return tr;}",
    	"Join":"function Join(r, delm) {if (typeof delm == \"undefined\") {delm = \" \";}return r.join(delm);}",
    	"Split":"function Split(s, delm, count, compare) {if (typeof delm == \"undefined\") {delm = \" \";}if (typeof count == \"undefined\") {count = -1;}if (compare) {var pat = new RegExp(delm, i);return s.split(pat, count);} else {return s.split(delm, count);}}",
    	"cDate":"function cDate(ud) {var d = new Date;var b;b = Date.parse(ud);d.setTime(b);if (d.getFullYear < 1920) {d.setYear(d.getFullYear + 100);}if (b) {return d;}return;}",
    	"cLng":"function cLng(n) {return parseInt(n);}",
    	"String$":"Array(a+1).join(b)",
    	"Space":"function Space(n) {return vb.String$(n, \" \");}",
    	"StrComp":"function StrComp(s1, s2, compare) {if (s1 == null || s2 == null) {return null;}if (compare) {if (s1.toLowerCase() == s2.toLowerCase()) {return 0;}} else {if (s1 === s2) {return 0;}}if (s1 < s2) {return -1;} else {return 1;}}",
    	"formatDateTime":"function formatDateTime(dat, formt) {var begs = \"\";switch (formt) {case 0:begs = dat.getMonth() + 1 + \"/\" + dat.getDate() + \"/\" + dat.getFullYear().toString().substr(-2);break;case 1:begs = dat.toLocaleString();break;case 2:begs = dat.getMonth() + 1 + \"/\" + dat.getDate() + \"/\" + dat.getFullYear().toString().substr(-2);break;case 3:begs = dat.toLocaleString().substr(-11).replace(/^\\s?/, \"\");break;case 4:begs = dat.toString().match(/\\d\\d:\\d\\d:\\d\\d/);break;default:;}return begs;}",
    	"formatNumber":"function formatNumber(Expression, NumDigAfterDec, IncLeadingDig, UseParForNegNum, GroupDig) {var oNum = Expression;var oStr = Onum.toString();var cNum = oNum;var cStr = oStr;var b;if (IncLeadingDig) {oStr.match(/^[\\.\\-]/) ? (cStr = \"0\" + oStr) : (cStr = oStr);}if (NumDigAfterDec) {oStr = cStr;cStr = cStr.split(\".\")[0] + \".\" + oStr.split(\".\")[1].substr(0, NumDigAfterDec);}if (GroupDig) {oStr = cStr;cStr = cStr.split(/\\d{3}/g);split(/([\\-\\$\\ \\d]{0,3})/g).join(\",\").substr(1).replace(/\\,\\,/g, \",\").replace(/\\,\\.\\,/g, \".\");}if (UseParForNegNum) {if (oNum < 0) {oStr = cStr;cStr = \"(\" + oStr + \")\";}}return cStr;}",
    };
    
    var ob=vb, out=out=blnPublishAsGlobals ? window : ob;
    for(var it in ob){
        var i=ob[it];
        if(i.match(/^function/)){
            out[it]=eval("("+i+")"); continue;
        }
      ob[it]=out[it]=Function("a,b,c,d", i.match("return")?i:("return "+i));
    }
    
    return ob;
    
    // make globals ?
    }(  true ));// set to false to have as methods of returned object

    example usage from http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
    Code:
    Mid("Mid Function Demo",14,4 ) // == "Demo";
    Last edited by rnd me; 01-07-2011 at 10:45 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts
    jayakodiu@yahoo.com

    Taking a clue from Mr.rnd me's response, the problem of other browsers noted by Mr.Philip may have a work-around as follows using "VBS function name" as a direct function with JS solution:

    <script>
    function instr(st,zt){return st.indexOf(zt)}
    function sgn(n){return ((n>0) ? 1 : -1)}
    function formatnumber(n,dn){return n.toFixed(dn)}

    alert(instr('asdfgh','df'))
    alert(sgn(2))
    alert(sgn(-2))
    alert(formatnumber(12.114,2))
    alert(formatnumber(12.115,2))
    </script>

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    In fact Javascript has perfectly good methods for most of the VBS functions you mention.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •