Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

[PHP] How do I execute javascript from PHP?



10:43 am on Mar 11, 2002 (gmt 0)

Hi again :)

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.



12:28 pm on Mar 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Hi 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.


12:31 pm on Mar 11, 2002 (gmt 0)


Do you mean:

include ("javascript:loadPage (xx, yy)")


Does this really execute a javascript?

I don't have any HTML-section in this file. The file is just for redirecting.

if (page == 'page1' && id == '23')
include ("page1.php?id=23");
exit ();



12:43 pm on Mar 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Nope. Is your javascript server side of client side?

[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.


12:54 pm on Mar 11, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Just got the extra bit on your post.

Your doing redirects.


Use php to build a redirect header.

print "Location: 301 [example.com...]

Or write print an html redirect.

print "
<meta http-equiv="Refresh" content="0;url=http://www.example.com/page.html?id=id">
<a href='http://www.example.com/'>Moved here</a> </body>

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.


2:00 pm on Mar 11, 2002 (gmt 0)

10+ Year Member

Have you tried the following?

if (isset($id))
else require_once("somefile1.incl");

Then its easy:

[somurl?id=5...] will include somefile5.incl, with on id (or id=0) will include somefile1.incl



Featured Threads

Hot Threads This Week

Hot Threads This Month