![](/img/trans.png)
[英]getting screen width into javascript variable and sending it through ajax to a php page to avoid page load
[英]Getting screen width value in a PHP variable using Ajax
我正在嘗試通過Ajax請求獲取PHP變量中的屏幕寬度。
JS檔案:
function screen() {
var xmlhttp;
var sWidth=window.screen.width;
if(window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "screen.php?width="+sWidth, true);
xmlhttp.send();
}
window.onload=screen();
PHP文件:
<?PHP
$screen=$_GET["width"];
echo $screen;
?>
但最后它顯示了未定義的“寬度”。 救命
var sWidth = window.screen.width;
alert("sWidth is: " + sWidth);
var xhttp = new XMLHttpRequest();
var url = "screen.php?width=" + sWidth;
xhttp.open("GET", url, true);
xhttp.onload = function() {
alert("Result: " + xhttp.responseText);
};
alert("Sending: " + url);
xhttp.send(null);
似乎工作正常。 FF或IE9中沒有問題http://jsfiddle.net/JsHaw/1/
這應該工作:
function screen() {
var xmlhttp;
var sWidth=window.innerWidth;
if(window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "screen.php?width="+sWidth, true);
xmlhttp.send();
}
window.onload=screen();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.