Ajax, add GET var onload



我希望,当我加载网页时,添加获取屏幕的宽度和高度,以便我的页面可以使用这些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(){ });函数中使用wrap代码。

使用此:

$(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 );
           });
   }
});

您只需要将jQuery添加到脚本中,然后在文档就绪功能中添加代码https://learn.jquery.com/ususe-jquery-core/document-/p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
$(function() {
    ratio();
    function ratio() {
        var Vorientation;
        if(window.screen.width>window.screen.height) {
            Vorientation = 1;
        } else {
            Vorientation = 2;
        }
        $.get("index.php", { orientation: Vorientation})
            .done(function(data) {
                alert("Data Loaded: " + data);
            });
    }
});

最新更新