将JavaScript变量传递给PHP-IE6、IE7和IE8中的错误
我正在尝试使用以下代码使浏览器视口托架将Javascript变量传递给PHP: 第一个代码将JavaScript变量传递给PHP-IE6、IE7和IE8中的错误,javascript,php,internet-explorer-7,internet-explorer-6,Javascript,Php,Internet Explorer 7,Internet Explorer 6,我正在尝试使用以下代码使浏览器视口托架将Javascript变量传递给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
<?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 "<script language='javascript'>\n";
echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=\" + screen.width + \"&height=\" + screen.height;\n";
echo "</script>\n";
exit();
}
?>
<script type="text/javascript">
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;
document.write('<p>Your viewport size is '+x+' x '+y+'</p>');
</script>
在网上搜索解决方案时,我发现了另一段代码:
第二个代码
<?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 "<script language='javascript'>\n";
echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=\" + screen.width + \"&height=\" + screen.height;\n";
echo "</script>\n";
exit();
}
?>
<script type="text/javascript">
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;
document.write('<p>Your viewport size is '+x+' x '+y+'</p>');
</script>
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth | e.clientWidth | g.clientWidth,y=w.innerHeight | e.clientHeight | g.clientHeight;
文档。写入(“您的视口大小为“+x+”x“+y+””);
这段代码显示浏览器视口,在包括IE6、7和8在内的所有浏览器中都可以正常工作
当我在一个php文件中同时运行这两个代码时,第一个显示为undefined
abd,第二个运行得很好。请看下面的截图
我不是一个新手程序员,无法将第二个代码连接到第一个逻辑。请帮助我这样做。
以下是所有浏览器的屏幕截图:
Internet Explorer 6
Internet Explorer 7
Internet Explorer 8
Internet Explorer 9
前箱
谷歌浏览器
<?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 "<script language='javascript'>\n";
echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=\" + screen.width + \"&height=\" + screen.height;\n";
echo "</script>\n";
exit();
}
?>
<script type="text/javascript">
var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;
document.write('<p>Your viewport size is '+x+' x '+y+'</p>');
</script>
歌剧
Safari
IE8及更低版本不支持window.innerHeight/Width。尝试使用
document.documentElement.clientHeight/document.documentElement.clientWidth
是否将这两个脚本组合起来?在您的标签中回显:
echo("var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;")
echo("location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=\" + x + \"&height=\" + y;\n";")
让我们用更漂亮的格式来布置第二个代码
<script type="text/javascript">
var w=window;
var d=document;
var e=d.documentElement;
var g=d.getElementsByTagName('body')[0];
var x=w.innerWidth||e.clientWidth||g.clientWidth;
var y=w.innerHeight||e.clientHeight||g.clientHeight;
document.write('<p>Your viewport size is '+x+' x '+y+'</p>');
</script>
因此,这里发生的事情是IE没有定义变量window.innerWidth
,因此您得到了未定义的。您的代码在此停止,而工作代码尝试其他一些变量,这些变量包括document.documentElement.clientWidth
(文档的宽度)和document.getElementsByTagName('body')[0]。clientWidth
(页面正文的宽度)
Internet explorer定义了其中的1个,这将为您提供正确的结果
回到您的代码:
将以下内容放在HTML页面的某个位置
<script type="text/javascript">
function GetScreenSize() {
var w=window;
var d=document;
var e=d.documentElement;
var g=d.getElementsByTagName('body')[0];
var x=w.innerWidth||e.clientWidth||g.clientWidth;
var y=w.innerHeight||e.clientHeight||g.clientHeight;
return {x:x, y:y};
}
</script>
echo " var sz = GetScreenSize()\n"; //Call the function to get the screen size
echo " location.href='${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}&width=' + sz.x + '&height=' + sz.x;\n";
函数GetScreenSize(){
var w=窗口;
var d=文件;
var e=d.documentElement;
var g=d.getElementsByTagName('body')[0];
var x=w.innerWidth | | e.clientWidth | | g.clientWidth;
变量y=w.内部高度| | e.倾斜度| | g.倾斜度;
返回{x:x,y:y};
}
echo“var sz=GetScreenSize()\n”//调用函数获取屏幕大小
echo“location.href='${u SERVER['SCRIPT\u NAME']}?${u SERVER['QUERY\u STRING']}&width='+sz.x+'&height='+sz.x;\n”;
谢谢安德鲁·布洛克,
你的代码也对我有用
<?php
echo "<br /><br />";
if (isset ($_REQUEST['width'])) {
$width=$_REQUEST['width'];
} else {
$width=0;
}
if (isset ($_REQUEST['height'])) {
$height=$_REQUEST['height'];
} else {
$height=0;
}
?>
<html>
<head>
<title> width=<?echo $width?> & height=<?echo $height?> </title>
<script type="text/javascript">
function GetScreenSize() {
var w=window;
var d=document;
var e=d.documentElement;
var g=d.getElementsByTagName('body')[0];
var x=w.innerWidth||e.clientWidth||g.clientWidth;
var y=w.innerHeight||e.clientHeight||g.clientHeight;
return {x:x, y:y};
}
var size=GetScreenSize();
</script>
</head>
<body>
<br />
width is <?echo $width?>
<br />
height is <?echo $height?>
</body>
</html>
<?php
if (!isset($_REQUEST['width'])&& !isset($_REQUEST['height'])){
echo "<script language='javascript'>\n";
echo " var sz = GetScreenSize()\n";
echo " location.href=\"${_SERVER['SCRIPT_NAME']}?${_SERVER['QUERY_STRING']}"
. "&width=\" + sz.x + \"&height=\" + sz.y;\n";
echo "</script>\n";
}
?>
宽度=&高度=
函数GetScreenSize(){
var w=窗口;
var d=文件;
var e=d.documentElement;
var g=d.getElementsByTagName('body')[0];
var x=w.innerWidth | | e.clientWidth | | g.clientWidth;
变量y=w.内部高度| | e.倾斜度| | g.倾斜度;
返回{x:x,y:y};
}
var size=GetScreenSize();
宽度是
高度是
你不需要把所有的电影放在这里。这不是论坛。解释一下就行了。@ThiefMaster:也许他必须为亚洲市场开发。例如,IE6就是一个例子。@Joseph谢谢你的建议。我是新来的,因此我仍在学习tenchiques演讲。我试图提供尽可能多的信息。下次我会记住这一点。顺便问一下,你对我的问题有什么解决办法吗?@T.J.Crowder感谢你有正确的市场知识,并正确地猜测了这个问题。不,你的代码中有一些systax错误。Dreamweaver显示为语法错误。感谢您的回复。我尝试将最后两行echo
包装在
标记中,这是在screenvar sz=GetScreenSize()location.href='/test.php?&width=1228&height=745&width='+sz.x+'&height='+sz.x代码>我遗漏了什么吗?