Forum Moderators: coopster
I use 'include' to include pages in a script depending on what the URL is.
I'd like to execute a javascript, maybe with the help of 'include'?
How do I do it?
The javascript expands a menu and loads a page in a frame.
/Grodan
Assuming you have external javascript files just print the link line as you would have in standard html.
print '<script language="JavaScript" src="utils.js"></script> ';
Or just include the line in the html section of your php file.
Now in the rest of your html you can reference the functions in utils.js.
In php you can do things like include differing javascript files based on browser or username etc. I do this with css files.
Do you mean:
include ("javascript:loadPage (xx, yy)")
Does this really execute a javascript?
Red.
I don't have any HTML-section in this file. The file is just for redirecting.
Like
if (page == 'page1' && id == '23')
{
include ("page1.php?id=23");
exit ();
}
/Grodan
[aside]I don't use javascript on the server side and am not sure if any hosting packages let you do this. Additionally - why use javascript to do something on the server side that php is better for?[/aside]
For client side javascript forget for a moment about PHP, just imagine that you were producing a plain old html page that runs clientside javascript. Work out where you would put your function calls, where you would put your links to external js files etc.
Now in your php files put exactly the same calls into the html that is produced by php. The browser only ever sees the html from the php script and will behave as if it got html with javascript in it.
As for running serverside javascript inside php - I don't know of a way to do this... though I would start looking at the functions that get html from URL's.
Your doing redirects.
Options:
Use php to build a redirect header.
print "Location: 301 [example.com...]
Or write print an html redirect.
print "
<html>
<head>
<meta http-equiv="Refresh" content="0;url=http://www.example.com/page.html?id=id">
</head>
<body>
<a href='http://www.example.com/'>Moved here</a> </body>
</html>";
Or you could use mod_rewrite to do similar things... do a site search for mod_rewrite here and you will masses of uses that you can put it to.
<?php
if (isset($id))
require_once("somefile$id.incl");
else require_once("somefile1.incl");
?>
[somurl?id=5...] will include somefile5.incl, with on id (or id=0) will include somefile1.incl
Skirril