Jump to content

PHP in JavaScript


shadowayex

Recommended Posts

Ok, so I am aware that it is possible to use PHP in an external JavaScript file if you save it as .js.php. But my issue is have a very simple three line code I want to put into my pages to redirect users. The code is this:

   <script type="text/javascript">	window.location = "http://hugdontmug.freehostia.com/<?php echo $page; ?>";   </script>

The script works, except the part where the $page variable (which is set on each page as the file name, if anyone was wondering) is written. Instead it redirects to http://hugdontmug.freehostia.com/, which in turn loads the index page. Is there any way of making this work without putting it in a .js.php file. If not, I'll settle for making a new file, but it seems rather pointless to create a new file for one piece of code.

Link to comment
Share on other sites

Ok, here's a chunk of code that uses it:

/*---------------Global Variables---------------*/$mode = mysql_real_escape_string($_GET['mode']);$id = mysql_real_escape_string($_GET['id']);$page = "messages.php"; <-----------------------------------------VARIABLE IS SET$account = mysql_query("SELECT * FROM users WHERE Username = '$user'");$account = mysql_fetch_array($account);$timezone = $account['Timezone']*3600;$timestamp = date("U")+$timezone;if($account['DST'] == "yes") { $timestamp = $timestamp+3600;}$time = date('g:i A \o\n F jS, Y', $timestamp);/*------------Messaging Functions------------*//*---------Delete Functions---------*/function deleteAll() { global $user; $deleteall = mysql_query("DELETE FROM messages WHERE Receiver = '$user'"); if(!deleteall) {  die("Error: Messages could not be deleted. Sorry for the inconvenience.");?>  <script type="text/javascript">   window.location = "http://hugdontmug.freehostia.com/<?php echo $page; ?>"; <-----------------REDIRECT  </script><?php }

That is from my messages.php page.

Link to comment
Share on other sites

You gotta scope problem because $page is referenced inside a function, making it local:Try changing this:window.location = "http://hugdontmug.freehostia.com/<?php echo $page; ?>";into this:window.location = "http://hugdontmug.freehostia.com/<?php global $page; echo $page; ?>";I don't remember (late at night) but if that doesn't work, try declaring $page as global in its first instance also. I'm in the ballpark, anyway. Very late, very late.

Link to comment
Share on other sites

Ok, so I am aware that it is possible to use PHP in an external JavaScript file if you save it as .js.php.
Oops i really dont know i can use PHP in an external JS file. THANKS A LOT :blush: So, is it that we have to
<script src="xxx.js.php"></script>

?And also, can we

<script src="xxx.js.php?var=data&var2=data2"></script>

?One more thing i wanna ask about JS (which i was wondering since i know JS)

<script src="xxx.js">// what to do with the codes i put here??</script>

If those are meaningless, why the syntax of JS including external files needs open and close tags, which provides a place in between! See how CSS

<link rel="stylesheet" type="text/css" href="mystyle.css" />

makes no confusions :)PHP in JS is SO COOL!! So, can i apply it to CSS?

<link rel="stylesheet" type="text/css" href="mystyle.css.php" />and<link rel="stylesheet" type="text/css" href="mystyle.css.php?wow=haha&damn=cool" />

cheersmidnite

Link to comment
Share on other sites

So, can i apply it to CSS?
<link rel="stylesheet" type="text/css" href="mystyle.css.php?wow=haha&damn=cool" />

Yes. Images too:
<img src="myimage.php?w=300&h=200&bg=#f8cc00" />

PHP, like all server-side technologies, process requests and send the responses back to the browser. If the response comes back as "text/css", then it can be used for CSS. If it comes back as "text/javascript", it can be used as javascript. If it comes back as "image/jpeg", it can be used as an image (JPG).

Link to comment
Share on other sites

Yes. Images too:
<img src="myimage.php?w=300&h=200&bg=#f8cc00" />

PHP, like all server-side technologies, process requests and send the responses back to the browser. If the response comes back as "text/css", then it can be used for CSS. If it comes back as "text/javascript", it can be used as javascript. If it comes back as "image/jpeg", it can be used as an image (JPG).

Oh? Do you mean even if <link rel="stylesheet" type="text/css" href="mystyle.php?wow=haha&damn=cool" /> (without the .css) will work also?And also, do i have to send back "text/css" header in the PHP script?
Link to comment
Share on other sites

Oh? Do you mean even if <link rel="stylesheet" type="text/css" href="mystyle.php?wow=haha&damn=cool" /> (without the .css) will work also?And also, do i have to send back "text/css" header in the PHP script?
Yes. It's the MIME type header that tells the browser what it is reading, not the extension. It's the same reason your PHP pages don't need to use the ".html" extension - PHP's default MIME type is text/html, which tells the browser it's reading an HTML document. Since you can change that, you could essentially deliver anything with PHP, from plain text, to images and binaries.Note that sometimes, applications are very hard coded as to not to respect MIME types. That's not the case with IE and CSS, JS and images, but a case in mind I have - I once tried to build a WPL playlist for a Windows Media Player object on a page. I used PHP to create the WPL file, and used a plain reference to it like "playlist.php?movie=2". It didn't work, even though I did adjusted the MIME type. I had to change the extension to ".wpl", and register that as an extension for PHP to process. Still, "playlist.wpl?movie=2" works like a charm AND it is PHP powered.
Link to comment
Share on other sites

Hi my friend boen_robot,So how can i change the MIME type of the response PHP page? And also where did you change the extension to ".wpl"? Lazy to dig into the docs :)Best,midnite

Link to comment
Share on other sites

So how can i change the MIME type of the response PHP page?
header("content-type:text/javascript"); //Or whatever

And also where did you change the extension to ".wpl"?
Umm... you can use the OS's tools to rename a file.
Link to comment
Share on other sites

Hi my friend boen_robot,So how can i change the MIME type of the response PHP page? And also where did you change the extension to ".wpl"? Lazy to dig into the docs :)Best,midnite
Umm... you can use the OS's tools to rename a file.
I tihnk he meant "How did you made the '.wpl' extension be processed by PHP?"...In Apache, at the place where you configure PHP, there should be a line like:
AddType application/x-httpd-php .php5 .php

This tells Apache that files with extensions ".php5" and ".php" must be given the application/x-httpd-php MIME type. That particular MIME type however corresponds to PHP being invoked, so you're essentially saying those extensions will trigger the PHP interpreter. So, to make ".wpl", or any other extension be parsed with PHP, you simply add it to this list like:

AddType application/x-httpd-php .php5 .php .wpl

Some people also like to have the ".html" extension itself be parsed by PHP. The deal is pretty much the same:

AddType application/x-httpd-php .php5 .php .wpl .html

If you're asking how I "changed the file extension from '.php' to '.wpl'?"... you know how to rename a file, right?

Link to comment
Share on other sites

Hi boen_robot,Ar Ha ::): Of course i was asking how did you configure the system such that .wpl can also be processed by PHP. Sorry for my incomplete question :)Is it a good idea to add AddType application/x-httpd-php .php5 .php .js .css? Can i skip the line header("content-type:text/javascript"); after adding .js?And also, i have a concern that some systems/proxy are caching webpages. Normally, .php are regarded as dynamic contents that will never be cached. On contrary, .html or things like .js, .css are most likely to be cached as they SHOULD BE static. So, i guess adding .js to type application/x-httpd-php won't be a good idea, isn't it? Yet, caching servers are naively picking the extensions (.htm or .php) or the content type in the hearders?Best,midnite

Link to comment
Share on other sites

I don't know about Proxies, but I believe that browsers are very tolerant in terms of JavaScript MIME types. The reason - there simply wasn't an official MIME type long ago. There is now applicaiton/javascript, but before it was text/javascript, so some user agents (browsers and proxies alike) have the same policies for both, and when neither MIME type is found, they just check if the extension is ".js" and assume JavaScript if it is. I think there was also application/ECMAScript... It's actually harder to block a file containing JavaScript than it is to run it.For CSS, I think (I'm not really sure) it's pretty much the same deal. If the MIME type is not text/css, check if the extension is ".css" and attempt CSS parsing if it is. Browsers at least do it. Proxies may only check the MIME type.I think IE has the weirdest type checking algoritm of all browsers. If memory serves, it first looks at the extension and if there's a MIME type associated with it in the registry. If there is, parse with the parser for that MIME type. If there isn't, or if the MIME type is not parsable by IE, check the HTTP header, and attempt parsing with that. If that too fails or is not parsable by IE, show a download box.As far as caching goes, you can always use more header() calls to control it by sending whatever caching headers necessary. It's a responsibility you must take when parsing any MIME type with any other.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...