Jump to content


Browser Width


2 replies to this topic

#1 meadow

    Young Padawan

  • Members
  • Pip
  • 224 posts
  • Location:Devon, England
  • Interests:Php, Hockey, mysql, web design.

Posted 11 November 2006 - 02:45 PM

How would I find out the browser width in PHP?

I have tried using php and javascript like this:

<?php
$width="<script type=\"text/javascript\">
var text=screen.width
document.write(text)
</script>"; 
?>

Which works fine, but when I try and divide or multiply that width like this, it doesn't work:

<?php
$width="<script type=\"text/javascript\">
var text=screen.width
document.write(text)
</script>"; 
$sWidth = $width/20;
$cWidth=$sWidth*19;
$numNav=$cWidth/100;
?>

Any ideas?

Edited by meadow, 11 November 2006 - 02:46 PM.


#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 11 November 2006 - 02:59 PM

You're getting ahead of yourself.
'tis impossible to do such a thing.

PHP is parsed by the server, JavaScript by the browser.
So what your doing is echoing the JavaScript, then trying to deal with an undefined integer (in this case you are trying to divide a string). Then, once the whole page gets to the browser, then the JavaScript is parsed; never beforehand.

I can think of a way to do this using AJAX, but would be a bit tedious. I'll explain it just incase it brings a spark to your mind.

First, echo the first bit you had, with JavaScript then grabbing the document size. Use an onload handler to call a function that will grab the window size, do your math, then use AJAX to send this to a PHP script, which will then do whatever you need with it. If needed, PHP can echo whatever data you need, then have the function update the page with that data.



Edit-
Then again, Matt has pointed someone else to a page relating directly on this subject.
http://php.net/manual/en/faq.html.php#faq....script-variable

Edited by Demonslay, 11 November 2006 - 03:08 PM.


#3 meadow

    Young Padawan

  • Members
  • Pip
  • 224 posts
  • Location:Devon, England
  • Interests:Php, Hockey, mysql, web design.

Posted 11 November 2006 - 04:21 PM

Cheers for the help and quick response, it works fine now.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users