Arun Prakash
Arun Prakash

Reputation: 9

how to get the returned value of a Javascript function executed at an HTML page

I am trying to develop a plugin to internet explorer browser using csharp and I try to inject a javascript to the loaded page. To inject the javascript i used the following code. The code is injected and the alert is working fine. but code given below should return the value of "msg" to output. when i run this code i get null value for output. kindly help.

var output= HTMLDocument.parentWindow.execScript("msg()","JScript");

function msg(){
    var msg = "This is sample";
    alert(msg);
    return msg;
}

Upvotes: 1

Views: 281

Answers (2)

irfan majeed
irfan majeed

Reputation: 61

IE cannot eval functions (Presumably for security reasons).

The best workaround is to put the function in an array, like this:

var func = eval('[' + funcStr + ']')

Upvotes: 0

jkd
jkd

Reputation: 1045

According to this page:

https://msdn.microsoft.com/en-us/library/ie/ms536420%28v=vs.85%29.aspx

The execCode method returns some sort of null value. Use eval if you want the value of msg().

Upvotes: 1

Related Questions