Detect browser screen resolution on the fly using AJAX?

Looks like the widescreen monitors will become more and more popular. For a 20" widescreen monitor, the default resolution is 1680 wide. This will affect how the web site look. My pony tail marketing people pointed out that my pages are too wide spread after looking at my 20" monitor.

My site are written in php. I am wondering if it is possible to send the screen resolution back to the server using AJAX before the body of the pages are sent. I have no idea about java script nor understand much about AJAX. I dislike the idea about relocating a page in order to send back the resolution. I will have to accept this if AJAX can not be used for this purpose. I need to consider the end users using non-java browser such as lynx.
Silent_DogAsked:
Who is Participating?
 
DireOrbAntConnect With a Mentor Commented:
I believe CSS will apply rules in order so this might work fine:

<HEAD>
<LINK HREF="Wide.css" REL="stylesheet" TYPE="text/css">
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if (screen && screen.width && screen.width < 1600)
  document.write('<LINK HREF="Normal.css" REL="stylesheet" TYPE="text/css">');
//-->
</SCRIPT>
</HEAD>

Then, make sure you override any styles that are wide in Wide.css using the same names in Normal.css. Make sense?

So Wide.css gets applied JavaScript or not, and Normal.css gets applied on top of it if JS is enabled AND width < 1600 (or whatever your max is).

I'm not sure it's the best way though, but worth a try :)
0
 
DireOrbAntCommented:
You could do something like this in the <HEAD></HEAD> block of your HTML page:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if (screen && screen.width && screen.width >= 1600)
  location.href = 'MyScript.php?Wide=1';
//-->
</SCRIPT>

It's not ideal at all, but much better than a trip back to the server using Ajax. Keep in mind that Ajax uses JavaScript, so JavaScript is required either way.

If at all possible, format your page using CSS and use JavaScript to decide what css file to use like this (again in HEAD block, and instead of the block above):
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if (screen && screen.width && screen.width >= 1600)
  document.write('<LINK HREF="Wide.css" REL="stylesheet" TYPE="text/css">');
else
  document.write('<LINK HREF="Normal.css" REL="stylesheet" TYPE="text/css">');
//-->
</SCRIPT>
0
 
Silent_DogAuthor Commented:
I like the idea of switching css file better than AJAX!

I understand AJAX uses java, what I need to do is having the page displayed as full width when the browser does not have javascript support.

Your example switches the css files. I need to give a default css file if the browser does not support javascript. Any idea?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Michel PlungjanIT ExpertCommented:
<NOSCRIPT>
<LINK HREF="Wide.css" REL="stylesheet" TYPE="text/css">
</NOSCRIPT>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if (screen && screen.width && screen.width < 1600)
  document.write('<LINK HREF="Normal.css" REL="stylesheet" TYPE="text/css">');
//-->
</SCRIPT>
0
 
DireOrbAntCommented:
mplungjan, that won't work. Wide.css will never be applied if they have 1600px and up and JavaScript disabled.

In order to use NOSCRIPT, do this:
<NOSCRIPT>
<LINK HREF="Wide.css" REL="stylesheet" TYPE="text/css">
</NOSCRIPT>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if (screen && screen.width && screen.width >= 1600)
  document.write('<LINK HREF="Wide.css" REL="stylesheet" TYPE="text/css">');
else
  document.write('<LINK HREF="Normal.css" REL="stylesheet" TYPE="text/css">');
//-->
</SCRIPT>

Note that I haven't tested NOSCRIPT inside the HEAD tag or if this would work everywhere (or at all).
0
 
Silent_DogAuthor Commented:
Thank you. I confirm the above method works.

I do however have a problem associated with it. This perhaps belongs to another thread, however it is still related to this topic. So any information will be appreciated.

My pages are marked up as "XHTML 1.0 Transitional". Firstly the validator complains about the <noscript> tag. I think I can get away by removing this. Secondly it appears using "document.write" does not seem to be the best thing with XHTML docs despite it works as web pages.

What will be the best practice? Would putting the script into a file solve the whole problem? What I might do is to include an in-line css style for the page width instead of switching the entire css file.
0
 
Michel PlungjanIT ExpertCommented:
" it appears using "document.write" does not seem to be the best thing with XHTML docs "
Appears how?
0
 
Silent_DogAuthor Commented:
It works when the pages are sent as text/html, it would fail when it is served as: application/xhtml+xml
Please see
http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite
0
 
Michel PlungjanIT ExpertCommented:
Did anyone actually test that?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.