sciencebear Posted April 20, 2010 Share Posted April 20, 2010 Hi all. I need some help. I need to get the width of a user's browser and then turn it into a php variable. I'm not exactly sure how to do this. Any help? Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted April 20, 2010 Share Posted April 20, 2010 Are you going to send the width off to php through a form or an ajax request? Because you can't detect the page width before it is loaded. To assume you are doing the latter, you can use the jQuery library to do a single, cross browser read, as outlined here: http://api.jquery.com/width/ Like so: $(window).width(); // returns width of browser viewport $(document).width(); // returns width of HTML document Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 20, 2010 Share Posted April 20, 2010 If you run a Google on "JavaScript browser width" you can find the snippet of codes for this. Quote Link to comment Share on other sites More sharing options...
sciencebear Posted April 21, 2010 Author Share Posted April 21, 2010 Hi again. I was able make a function GetWidth() that returns the variable I need. I can make it show in html by using this code: <script type="text/javascript">document.write(GetWidth())</script> However, I need to use that value in PHP. How can I define that as a PHP variable? Edit: I am only using it to define the width of a div tag (It's not just 100%, GetWidth performs some other operations to the browser width). If there's an easier way to do this without PHP, I could do that as well. Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted April 21, 2010 Share Posted April 21, 2010 You can do something like: <script type="text/javascript"> document.getElementById('myDiv').style.width=getWidth(); </script> Quote Link to comment Share on other sites More sharing options...
sciencebear Posted April 21, 2010 Author Share Posted April 21, 2010 That worked exactly how I needed it to. Thank you very much! Quote Link to comment Share on other sites More sharing options...
seventheyejosh Posted April 21, 2010 Share Posted April 21, 2010 No problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.