1

I would like, when i'm loading a webpage, to add in GET the width and height of the screen, so that my page can use these var to redirect to a specific page.

I've looked for the function and it seems that I should use window.screen.width and window.screen.height

Also, I figured out I should use $.get() to send the variables.

So I came out with this script, but i assume i'm mixing JS and AJAX which I don't if it's ok or not. And most of all, I don't know how to run the function when my page is loading.

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

Here is the html :

    <body>

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

</body>
2
  • 2
    mixing js and ajax: AJAX is not a language, it's a way of retrieving a response from a server (e.g. from PHP code) with javascript, often used to generate content on the page based on user input. $.get is jQuery (which is a js lib) to perform a ajax GETrequest Commented Dec 16, 2016 at 8:34
  • 1
    @asdf_enel_hak Actually, the way the OP's using it, it's a global variable. You're right though; it should be declared before the if like var Vorientation;
    – qxz
    Commented Dec 16, 2016 at 8:36

2 Answers 2

2

To run the function when your page is loading, you have to wrap code in $(document).ready(function(){ }); function.

Use this:

$(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 );
           });
   }
});
2
  • I think Vorientation need to be declared outside of the if/else no ?
    – EQuimper
    Commented Dec 16, 2016 at 8:38
  • 1
    It needs to be declared in the first place. Using a global isn't right here
    – qxz
    Commented Dec 16, 2016 at 8:39
2

You just need to add jQuery to your script and add your code in the document ready function https://learn.jquery.com/using-jquery-core/document-ready/

<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);
            });
    }
});
24
  • 1
    AJAX requests happen in the background. They don't change the URL in your browser's address bar; they just return the response data to your callback function (data here).
    – qxz
    Commented Dec 16, 2016 at 8:43
  • 1
    Do you ask how can you get the value of Vorientation on server-side ? Commented Dec 16, 2016 at 8:51
  • 1
    can you console.log(Vorientation) before the ajax ? just for be sure this is not the problem. I don't know how you handle your server :(
    – EQuimper
    Commented Dec 16, 2016 at 8:53
  • 1
    @Ezhno, youy need to use this: $_POST['orientation'], not $_POST['Vorientation'], because you send with this: { orientation: Vorientation} Commented Dec 16, 2016 at 8:55
  • 2
    If your PHP is getting values from the query string, try $.get( "index.php?orientation="+Vorientation ). $_POST won't work with this client code because you're sending a GET request.
    – qxz
    Commented Dec 16, 2016 at 8:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.