PDA

View Full Version : document.write and php


Cherubae
03-12-2003, 03:41 PM
//start newbie javascript question

I'm trying to change my page layout depending on a user's screen resolution via javascript. If a user is 1024x768, I want a script that scrolls aross the top of the page to apear. If they don't meet the requirements, I don't want the script to appear.

I figured I could use the screen.width to help me determine what the user's screen size was and then display the file imported via php. Unfortunatly it's not working and I receive a "parse error, unexpected T_CONSTANT_ENCAPSED_STRING " message. I've got different style sheets being used depending on resolution but I can't get this script to work:


<script type="text/javascript">
<!--
if ((screen.width >= 1024)){
document.write('<div id="top">');
document.write('<?php');
document.write('include \'cloud_scroll.php\';');
document.write('?>');
document.write('</div>');
}
-->
</script>


If I add an escape character before the "?" ('<\?php') ) ,that gets rid of the parse error but the php portion of it doesn't work and the cloud_scroll.php file isn't included.

Is the problem with the way I'm using document.write?

// end newbie quesiton


- C

Borgtex
03-12-2003, 03:50 PM
I think that it can't work the way you want, as php is a server side language and you can't call it from javascript, that it's a client side language (that is, executed by the browser)

Cherubae
03-12-2003, 03:53 PM
That makes sense as to why it's not working then :D

cheesebagpipe
03-12-2003, 10:57 PM
You might want to use an iframe for this...if the resolution test is passed, document.write the HTML for the iframe (embed the script in the <body>) which will allow you to download the PHP script into the iframe for display. As noted, a PHP include is meaningless on the client.

Careful:

}
//-->
</script>