Board index   FAQ   Search  
Register  Login
Board index php forum :: HTML JavaScript

screen resolution and variable type

Javascript coding ..

Moderators: macek, egami, gesf

screen resolution and variable type

Postby KarterJK » Thu Aug 21, 2003 10:44 am

I wish to check the screen resolution and switch pages depending on that. I am using this test code but, my logic escapes me why it doesnt work properly
my screen is set to 1024

<?php
$a = (string) 1024;

$screenwidth = (string) "<script language=\"Javascript\">document.write(screen.width);</script>";
echo $screenwidth;
echo $a;


if ($screenwidth == $a){
echo 'yes';
}
else {echo 'no';}

?>
when it prints it displays this:

10241024no

I do not understand why they are not equal
I used the string function to try and force the same type

I am learning PHP but I am perplexed by its logic

Thank you in advance
KarterJK
New php-forum User
New php-forum User
 
Posts: 21
Joined: Tue Aug 05, 2003 11:19 am

Postby liquedus » Thu Aug 21, 2003 11:39 am

PHP is processed by the server and then passed to the client as HTML, JavaScript is only executed on the client machine... so if you are trying to use javascript in you PHP it wont work.


you can redirect by just using javascript:

if(window.screen.availWidth=="1024")window.location.href="here.html";
else window.location.href="there.html";

:wink:
liquedus
php-forum Active User
php-forum Active User
 
Posts: 266
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada

Postby swirlee » Thu Aug 21, 2003 11:57 am

As a side note, this is pretty much universally a bad idea. Don't ever resize your users' window, and do make your site work with any resolution, at least 800x600. It's just good design and common sense.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby KarterJK » Thu Aug 21, 2003 1:43 pm

Well,
I led you astray a bit the purpose of the code is to allow me to write the proper html headers to the current page. I dont want to redirect them to another page.

I know how to do all of that in javascript. No Problem.

Here is the jist of my index page is to set up a frames page according the screen resolution. If its one resolution I want to set up frames one way and if its not I want to set up frames another.

Yes, I can use java to redirect them. But, my reasoning is if the screen resolution can be checked in my php simply write the appropriate HTML header on-the-fly so to speak. Eliminating 2 additional html pages. I hate having thousands of small additional pages.

Ummmm, <thinking>

I am still confused as to why the $screenwidth is not a viable php variable? The content is stored in $screenwidth, why then can I not use it for a comparison check? That loses me.
To me its just like a variable submitted by a form to a PHP process.
KarterJK
New php-forum User
New php-forum User
 
Posts: 21
Joined: Tue Aug 05, 2003 11:19 am

Postby swirlee » Thu Aug 21, 2003 2:28 pm

KarterJK wrote:I am still confused as to why the $screenwidth is not a viable php variable? The content is stored in $screenwidth, why then can I not use it for a comparison check? That loses me.
To me its just like a variable submitted by a form to a PHP process.


PHP is a server-side technology -- all of the processing is done on the server. JavaScript (JScript, ECMAScript, et al) is a client-side technology -- all of the processing is done on the user's machine after the page is downloaded. When a user's browser requests a PHP page, the script is run on the server and then sent to ther user. By the time the user's browser recieves it and has a chance to process the JavaScript, the PHP processing is already done and there's no way that that PHP can access values that are the result of the JavaScript code.

Do me a favor and run the code you posted above in your browser. Now go up to View > Source. You'll see that the output of your script is not this:

Code: Select all
10241024no


Rather, the output of your script is this:

Code: Select all
<script language="Javascript">document.write(screen.width);</script>1024no


At no point is PHP even the slightest bit aware of the value that your JavaScript outputs.

Now, then, if you're using frames, why not just use JavaScript (skip the PHP) to write the contents of the frameset? I won't post example code, as I loathe frames.

And I still think this is a bad idea. Unless you're writing a web app like Oddpost or something and can justify resolution-dependent layouts, it's a far better strategy in the end to make a single design that works equally well regardless of resolution.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby KarterJK » Fri Aug 22, 2003 8:21 am

Ahhh,

Now I get it, the first 1024 is the actual document.write statement I am seeing and not the echoed $screenwidth (fooled by my own program). I have only worked with PHP for a month

Yes, I can do it in javascript. But, this was an exercise for me to learn PHP, I was trying to write as much of it in PHP as possible. Its only a 2 frame page http://www.karterjk.com I have a chat window that can be brought up. Its for our gaming clan. The menu control is at the bottom except if in 800 resolution I put it on top. I could leave it on top, but, so many clans copied my old layout, That I wanted to be just a little different.

Thank you very much for your explanation.
KarterJK
New php-forum User
New php-forum User
 
Posts: 21
Joined: Tue Aug 05, 2003 11:19 am

a meta page

Postby ruturajv » Fri Aug 22, 2003 11:03 pm

Put a page where a javascript runs and gets the variables of screen reolution,
Put the variable values into <input type=hidden width=variable> and so, no add the
<meta http-equiv> tag and take it to a new php page and retrieve the contents through $_GET['varname']

not tested it but hope it works,
don't really have time to write the code sorry :(
maybe swirlee can help :lol:
User avatar
ruturajv
php-forum Super User
php-forum Super User
 
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India


Return to JavaScript

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.