www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Browser Width

  1. #1
    Join Date
    Oct 2012
    Posts
    11

    resolved [RESOLVED] Browser Width

    Hello,

    I would like to find out the browser width's numeric value.

    Code:
    <?php
    $width = " <script>document.write(window.outerWidth); </script>";
    echo $width;
    Gets the value but say i try add 1 to it

    Code:
    <?php
    
    echo $width + 1;
    This just prints 1.... since i wanna add it to an if statement like this

    Code:
    if ($width < 1280){
    
    $bla = "this";
    
    }
    else {
    $bla = "that";
    }

  2. #2
    Join Date
    Dec 2012
    Posts
    21
    you're mixing languages here, it can be confusing at the beginning.

    the first bit is php; that's run on the server; think of it as preparing the contents of the page for the user; it pulls templates together, loads in data, lays out the page etc.

    but browser width is a client issue; so to do that you need to do it with javascript.

    you've started to do that; your php outputs some javascript which determines and prints the window width. that's fine, except there's no way to get that value from the javascript back into php; because by the time that javascript starts executing, the php has already finished executing; its too late.

    so what you need to do is put the logic you're trying to achieve (if this.. else...) into javascript.

    if you really, really, really had to have the window width value on the server side, then there are cumbersome and ways of doing this, but for the most part, between CSS and Javascript, you should be able to achieve what ever layout you're trying to achieve.

    hope that helps!

  3. #3
    Join Date
    Oct 2012
    Posts
    11
    Thanks heaps, explained well!

    Yeah i know a bit of PHP and like no JS so was kinda trying to get everything done by PHP....

    What i ended up using was CSS Media Query since the end goal i had was to show different content depending on screen resolution....

    Works well! need hit the books on JS tho.....

    Thanks heaps though!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles