由于 javascrīpt (通常情况下)是客户端技术,而 php (通常情况下)是服务器端技术,而且 HTTP 是一种“无状态”协议,因此两种语言之间不能直接共享变量。
但是,有可能在二者之间传递变量。一种实现的方法是用 PHP 生成 Javascrīpt 代码,并让浏览器自动刷新,将特定的变量传递回 PHP 脚本。以下例子显示了如何这样做――让 PHP 代码取得显示屏幕的高度和宽度,通常只能在客户端这么做。
<?php
if (isset($_GET['width']) AND isset($_GET['height'])) {
// output the geometry variables
echo "Screen width is: ". $_GET['width'] ."<br />/n";
echo "Screen height is: ". $_GET['height'] ."<br />/n";
} else {
// pass the geometry variables
// (PReserve the original query string
// -- post variables will need to handled differently)
echo "<scrīpt language='javascrīpt'>/n";
echo " location.href=/"${_SERVER['scrīpt_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=/" + screen.width + /"&height=/" + screen.height;/n";
echo "</scrīpt>/n";
exit();
}
?>
新闻热点
疑难解答