javascript - Ajax,添加 GET var onload

标签 javascript php jquery ajax

<分区>

我想在加载网页时在 GET 中添加屏幕的宽度和高度,以便我的页面可以使用这些 var 重定向到特定页面。

我找过函数,看来我应该使用 window.screen.widthwindow.screen.height

此外,我发现我应该使用 $.get() 来发送变量。

所以我想出了这个脚本,但我假设我正在混合 JS 和 AJAX,不管是否可行,我都不会这样做。最重要的是,我不知道如何在我的页面加载时运行该功能。

    $(document).ready(function() {
    ratio();
function ratio() {
    var Vorientation;
    if(window.screen.width>window.screen.height)
    {
       Vorientation = 1;
    }
    else
    {
       Vorientation = 2;
    }
    $.get( "index.php?orientation="+Vorientation );
    alert("ok");
 }
  });

这是 html :

    <body>

<?php 
if(isset($_GET['orientation']))
{
echo"ok";
} 
?>

</body>

最佳答案

要在页面加载时运行该函数,您必须在 $(document).ready(function(){ }); 函数中包装代码。

使用这个:

$(document).ready(function(){
    window.Vorientation=0;
    ratio();
    function ratio() {
        if(window.screen.width>window.screen.height)
        {
           Vorientation = 1;
        }
        else
        {
           Vorientation = 2;
        }
        $.get( "index.php", { orientation: Vorientation} )
           .done(function( data ) {
              alert( "Data Loaded: " + data );
           });
   }
});

关于javascript - Ajax,添加 GET var onload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41180186/

相关文章:

javascript - 我们能知道用户从哪个页面访问了php中的这个页面吗?

javascript - 加载 HTML 格式的大型视频文件

PHP - 在视频上传期间提取帧

php - Codeigniter 是否有可扩展的多站点策略?

javascript - 如果选择特定单选按钮如何显示表单

javascript - Firefox - 在 contenteditable 中聚焦一个段落

javascript - 定义 : "this.isStartDateSet" vs. "!this.isStartDateSet"

javascript - 如何将一些丑陋的内联 JavaScript 转换为函数?

当正文中有 url a 标签时 PHP 邮件无法发送

Javascript替代高度、宽度控制