Execute JavaScript in a html page?

Feb 17, 2012 at 8:21 PM

Hey.

I have a html page with javascript in it. When I'm pressing the submit button in my browser a variable is calculated and submitted to the server.

Now I 'm trying to get the value of this variable without pressing the button or using a browser. Therefore I downloaded the JavaScript for .Net.

How can I simply get the value of this variable? I deleted all html stuff, so theres only the script without start (<script type="text/javascript">) and endtag </script>.

I'm a noob in JavaScript.

My first problem is, that I have to specify the document, so I tried the following:

            string html = File.ReadAllText("index.htm");
            // Initialize a context
            JavascriptContext context = new JavascriptContext();
            context.SetParameter("document", html);
            // Script
            string script = File.ReadAllText("script.js");
            // Running the script
            context.Run(script);
            // Getting a parameter
            var val = context.GetParameter("variable");

 

Developer
Feb 19, 2012 at 8:59 PM

Javascriptdot contains just JavaScript, and not any other pieces of the web browser.  Therefore it does not know how to extract JavaScript from HTML.  Nor can it read HTML and construct the DOM for your JavaScript to read/manipulate.

  Oliver

Feb 20, 2012 at 11:45 AM
Edited Feb 20, 2012 at 11:45 AM

Yeah thats right. But to run the script I have to specify the document, otherwise I get a NullReferenceError.
Now it would be interesting to know how to do that.

Christian

Apr 27, 2012 at 3:39 PM

This could help you.  https://github.com/thatcher/env-js

This provides a javascript file which creates browser provided objects like Window, Document etc into the Context. After you load env-js into context you can open your html using Window object. 

May 9, 2012 at 3:49 PM
Edited May 9, 2012 at 4:07 PM

 There is a javascript function

(function (t, s, r){ - 1500 lines code- ...........
})(window, window.document);

Can I use the so- javascript.net
public class window
{
public bool closed
{
... }

publick int length
{
...}

Etc.

similarly for document(dom) and call

window w = new window();
context.SetParameter("window",w);
context.SetParameter("document",w.document);