Jump to content

how do JS function run with eval?[Solved]


Recommended Posts

Quote

function typeObj(obj){
      var type=Object.prototype.toString.call(obj);
      if(type=='[object Array]'){
        return 'Array';
      }else if(type=='[object  Object]'){
        return 'Object';
      }else{
        return "obj is not object or array"
      }
}

As above javascript function code.
I want to run the code with eval to get the results.
As follows:

#include <IE.au3>

Global $oIE = _IECreate("about:blank")

Local $sS = ""
$sS &= 'function typeObj(obj){'
$sS &= '      var type=Object.prototype.toString.call(obj);'
$sS &= '      if (type==' & "'" & '[object Array]' & "'" & '){'
$sS &= '        return ' & "'" & 'Array' & "'" & ';'
$sS &= '      }else if (type==' & "'" & '[object  Object]' & "'" & '){'
$sS &= '        return ' & "'" & 'Object' & "'" & ';'
$sS &= '      }else{'
$sS &= '        return "obj is not object or array"'
$sS &= '      }'
$sS &= '};var x=new typeObj(' & $oIE & ');return x;'

ConsoleWrite("JSEval : " & JSEval($sS) & @CRLF)

_IEQuit($oIE)
$oIE = 0

Func JSEval($str) ;Javascript表达式求值
    Local $sRet = ""
    With ObjCreate("MSScriptControl.ScriptControl")
        .Language = "javascript"
        $sRet = .Eval($str)
    EndWith
    Return $sRet
EndFunc

But but I get an error:

Quote

$sRet = .Eval($str)
$sRet = ^ ERROR

Thanks in advance!

Edited by Letraindusoir
Link to post
Share on other sites

Here one way to create a JS object in IE :

#include <Constants.au3>
#include <IE.au3>

Global $oIE = _IECreate("about:blank")
$oIE.Document.parentWindow.execScript("document.body.oAutoIt = eval;")
Global $eval = Execute("$oIE.Document.body.oAutoIt")
If Not IsObj($eval) Then Exit MsgBox ($MB_SYSTEMMODAL,"","Error creating JS object")
MsgBox($MB_SYSTEMMODAL, "Eval", $eval('typeof 5')) ; execute js and get return
$eval("alert('Test');")

 

Link to post
Share on other sites

Thank you two great master for paying attention to this small problem!
Now I'm learning to transform some JS functions in Au3 ways to verify that the same results can be obtained
A few minor syntax errors in the previous post have been corrected: elseif-->else if
Now I know why I was wrong.The Correct code is as follows:

#include <IE.au3>

Global $oIE = _IECreate("about:blank",0,0,1,0)

Local $sS = ""
$sS &= 'function typeObj(obj){'
$sS &= '      var type=Object.prototype.toString.call(obj);'
$sS &= '      if (type==' & "'" & '[object Array]' & "'" & '){'
$sS &= '        return ' & "'" & 'Array' & "'" & ';'
$sS &= '      }else if (type==' & "'" & '[object  Object]' & "'" & '){'
$sS &= '        return ' & "'" & 'Object' & "'" & ';'
$sS &= '      }else{'
$sS &= '        return "obj is not object or array"'
$sS &= '      }'
$sS &= '};typeObj(123);'

ConsoleWrite("JSEval : " & JSEval($sS) & @CRLF)

_IEQuit($oIE)
$oIE = 0

Func JSEval($str)
    Local $sRet = ""
    With ObjCreate("MSScriptControl.ScriptControl")
        .Language = "javascript"
        $sRet = .Eval($str)
    EndWith
    Return $sRet
EndFunc
Quote

var x=new typeObj(' & $oIE & ');return x; <----Syntax Error

 

Edited by Letraindusoir
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...