[SOLVED] PHP and JAVA Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tstimple
Forum Commoner
Posts: 53
Joined: Wed Jan 21, 2004 10:12 pm

[SOLVED] PHP and JAVA Script

Post by tstimple »

Before I get into all the details...

Is it possible to dynamically build JAVA Script on a web page using PHP?

--Tim
Last edited by tstimple on Tue Nov 02, 2004 5:49 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Certainly.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Building PHP is like assuming PHP is an "HTML editor", it just adds text to a "file", even if the "file" is really just memory. All you need to do is output the script as though it was HTML..

Very Simple Example:

Code: Select all

<?php
echo('<html>
<head>
<title>Pointless test</title>
</head>
<body>
<script language="javascript">
function writeText(mytext)
{ document.write(mytext); }
</script>
<script language="javascript">alert("Hello World");</script>
  <p> Well This is written by PHP </p>
  <script language="javascript">
  writeText("This is HTML written via javascript function");
<noscript>This dummy needs Javascript</noscript> 
</body>
</html>');
?>
Post Reply