1

I want to get the result of shown JavaScript and HTML code. For example, when we write document.write("Hello"), return "Hello". Look at this code:

$html_code = <<<EOL

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>Hello World</title>
        </head>
        <body>
            <script>
                document.write("Hello World");
            </script>
        </body>
    </html>

EOL;

$result = getResult($html_code);

echo $result;

I want to get this result:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Hello World</title>
    </head>
    <body>
        Hello World
    </body>
</html>
7
  • 1
    You can Directly write html into php file Commented Jan 22, 2016 at 15:55
  • stackoverflow.com/questions/15775303/… Commented Jan 22, 2016 at 15:59
  • To clarify, are you trying to print the raw html, or are you trying to display the result of the rendered html? Commented Jan 22, 2016 at 16:01
  • I need something like HtmlUnit (that is for Java) for PHP. Commented Jan 22, 2016 at 16:28
  • You need to be more specific about what exactly you want and what you want to use it for. I don't understand your question! Commented Jan 22, 2016 at 20:06

1 Answer 1

1

I understand that you want to get the result of a HTML page with embedded Javascript code on the server side, in the same script that generates this HTML and Javascript code.

You can not!

The reason

  • The PHP script runs on the server side.
  • The HTML code (with the embedded Javascript code) has to be interpreted by a browser on the client side.
  • The browser would execute the Javascript code. And only then the Javascript code (document.write("Hello World");) would create the "Hello world" HTML text. And only then the HTML text would look like you are expecting.

In brief: you expect a result on the server side which had to be produced on the client side before.

In your example you could directly have the PHP script generate the "Hello world" text - no need for Javascript.

Sign up to request clarification or add additional context in comments.

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.