在PHP中执行JavaScript [英] Execute javascript in PHP

查看:75
本文介绍了在PHP中执行JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP生成典型的Web 2.0 HTML页面:它包含大量的< script> 标签和javascript代码,加载事件。

有没有办法直接从PHP获取最终的HTML代码,而无需使用任何浏览器打开页面?



例如,假设页面的HTML为(仅仅是一个例子):

 < html> ; 
< head>
< script> ... jquery库代码...< / script>
< script> $(document).ready(function(){$(body)。append(< p>嗨!< / p>);< / script>
< / head>
< body>
< / body>
< / code< / pre >

这个HTML保存在 $ html PHP变量中。现在,我想将该变量传递给某个函数,该函数将返回< html>< body>< p>嗨!< / p>< / body>< / html>



这可能吗?



编辑:因为你们中的许多人感到困惑我的请求我会解释原因,不幸的是,所有用户面对的都是用javascript编写的,这使得网站无法通过搜索引擎进行搜索,所以我想给他们发送事后准备好的HTML代码。

解决方案

要使用PHP评估JavaScript代码,请查看 V8 JavaScript引擎扩展你可以将它编译成你的PHP二进制文件: b
$ b



V8是 Google的开源JavaScript实施


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.

解决方案

To evaluate JavaScript code using PHP, have a look at the V8 JavaScript engine extension, which you may compile into your PHP binary:

V8 is Google's open source JavaScript implementation.

这篇关于在PHP中执行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆