views:

200

answers:

7

I'm generating your typical Web 2.0 HTML page with PHP: it contains a lot of <script> tags and javascript code that will substantially change the DOM after the load event.

Is there a way to get the final HTML code directly from PHP, without opening the page with any browser?

For example, let's say the HTML for the page is (it's just an example):

<html>
<head>
<script>...the jquery library code...</script>
<script>$(document).ready(function() { $("body").append("<p>Hi!</p>");</script>
</head>
<body>
</body>
</html>

This HTML is saved in the $html PHP variable. Now, I want to pass that variable to some function that will return $result = <html>....<body><p>Hi!</p></body></html>.

Is this possible?

EDIT: since many of you were perplexed by my request I'll explain the reason. Unfortunately everything user facing was made in javascript and this makes the website uncrawlable by search engines. So I wanted to send them the post-ready event HTML code instead.

A: 

Yes that should work from what I can tell.

jini
This HTML is saved in the $html PHP variable. Now, I want to pass that variable to some function that will return $result = <html>....<body><p>Hi!</p></body></html>.Is this possible?Yes you can pass the variable as a function. That is what you asked for in your question. A little more clarification would have been nice.
jini
+1  A: 

No this is not possible. Javascript is executed on the client side and php on the server side. So as soon as the javascript part gets executed (in the browser of the visitor) the php stuff is already completed.

Besides that, i couldn't imagine why you want to do it like in your example.

TheCandyMan666
Javascript is not "magic", it has an interpreter / compiler / whatever that executes the code, which could run server side.
Andreas Bonini
See above: things like "window.close" or "alert" or anything else that may require user interaction doesn't make sense server sided.
Select0r
The functions that don't make sense would do nothing. Also there is no "above" or "below": on Stack Overflow the posts are ordered by votes, what is above now may not be tomorrow.
Andreas Bonini
+3  A: 

If I understand you right, you'd like to execute a JavaScript-function in PHP ... JavaScript is executed in the browser (client side), PHP is server-sided, so unless you write a JavaScript-parser in PHP, that won't work.

Why a JS-parser on the server would make sense at all (I can't think of a reason why it should) or is possible in the first place, is another question ... JS will work on a DOM that doesn't exist on the server as well as functions are called that are useless (think of what "window.close()" would/should do on the server!?).

So to make the answer short: No. :)

Select0r
yes, I'm asking if a PHP javascript interpret exists. The DOM can exist on the server too, why couldn't it? Functions like `window.close()` would do nothing.
Andreas Bonini
I don't know of a PHP JS interpreter and I still doubt its sense. If you execute something on the server, why use a client-sided language and not use PHP all the way?
Select0r
I edited my post to include the reasoning behind this crazy idea
Andreas Bonini
+1  A: 

It would be possible if you had a javascript interpreter built into PHP (or at least something on the server you could call to interpret the HTML with javascript embedded). There have been some attempts (eg http://j4p5.sourceforge.net/index.php), but I would steer clear of these and try to rethink what you're doing. Depending on your specific needs, templating (ie. something like Smarty) might be able to solve part your problem (it will however NOT interpret javascript of course).

wimvds
+1  A: 

The best solution that I could find is to use HtmlUnit http://htmlunit.sourceforge.net/ on the server to execute your html with the javascript and get back the final html that the user would see on the browser.

The library has good support for JavaScript and is headless so you should be able to run it on the server.

You would need to write a small Java wrapper that could accept input via the command line and pass it onto HtmlUnit for processing and then return the result to you. You could then call this wrapper from PHP.

Sijin
A: 

I doubt that there are some good general purpose server-side runtimes for browser JavaScript generally and in PHP specifically. For complicated client scripts there is no such thing as "final DOM state". Imagine that some DOM-updating method is scheduled with setTimeout. Do you want to wait for it? And if it reschedules some other update in the same way (for example just to show current time somewhere on the page), how long are you going to wait? And what if page does some AJAX data downloading? Do you want to do actual server requests, emulate cookies, etc.? I think this is all too complicated to be implemented in a good way. Well, maybe Google has something like this in their crawler, but is it specialized for their particular needs.

iPhone beginner
A: 

There are new servers that run Javascript server-side and are able to manipulate the DOM but it has nothing to do with PHP .

http://jaxer.org/

jef2904