0

I am establishing a HttpUrlConnection with a webpage . Now I have javascript snippet that I want to execute on this webpage . How can I do this ?

4
  • 2
    javascript gets executed in a browser, so you could use a browser automatisation framework like for example webdriver/selenium Commented Mar 28, 2014 at 15:05
  • Do you control the webpage? You cannot execute script directly, as JavaScript is executed client side, but you could code something that listens for incoming connections. Commented Mar 28, 2014 at 15:06
  • this is my website . i control it . but i wanted to execute javascript on it as a client . Commented Mar 28, 2014 at 15:08
  • stackoverflow.com/questions/247483/… is this what you looking for? Commented Mar 28, 2014 at 15:09

2 Answers 2

2

You could try :

    private static ScriptEngineManager mgr = null;
    private static ScriptEngine engine = null;

    mgr = new ScriptEngineManager();
    engine = mgr.getEngineByName("JavaScript");

    Object eval = engine.eval(s); // s is javascript code
Sign up to request clarification or add additional context in comments.

Comments

1

Since Javascript needs some kind of Virtual Machine to run in - usually provided by browsers or programms which act like browsers (so called headless browsers), you have to use some kind of "browsing engine" (so to say) to run the page in. Perhaps WebEngine may be worth a look.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.