Page 1 of 1
How to include javascript in a php page?
Posted: Sun Aug 24, 2008 10:02 pm
by gyclone
Howdy,
I am building my first PHP site and I have a cool javascript snippet to disable links to the current page. The code works great in plain html or ASP, but doesn't function in php. I have spent all day googling and reading forums trying to figure out the rules for injecting JavaScript into a PHP page and haven't really gotten anywhere. The examples I've found seem to contradict one another (or I'm just too ignorant to implement them properly).
My code is as follows:
Code: Select all
<script type="text/javascript" language="javascript">
<!--
function manageLinks()
{
var link;
for (var i = 0; (link = document.getElementsByTagName('a')[i]); i++)
{
if (link.href.indexOf(location.href) != -1)
{
document.getElementsByTagName('a')[i].removeAttribute('href');
}
}
}
window.onload = function() {
manageLinks();
}
-->
</script>
If someone knows a way to disable links to the current page with PHP, I'd love to hear it, but I really need to know the rules for injecting JavaScript into a PHP page, since there are other snippets I'd like to use.
Thanks!
Re: How to include javascript in a php page?
Posted: Sun Aug 24, 2008 10:20 pm
by Christopher
Put that in a file named something like "managelinks.js" an using PHP do:
Code: Select all
<html>
<head>
<?php include 'path/to/managelinks.js'; ?>
</head>
<body>
...
Re: How to include javascript in a php page?
Posted: Sun Aug 24, 2008 10:46 pm
by gyclone
Thanks for the reply!
Unfortunately, that doesn't seem to work for me (no error message, it just doesn't have any effect).
I'm using kind of a master page/content page set up involving output buffering, I don't know if that may be the problem.
Here's an abridged version of the code:
Content Page
Code: Select all
<?php
//Buffer larger content areas like the main page content
ob_start();
?>
<p>Some content</p>
<?php
//Assign all Page Specific variables
$pagemaincontent = ob_get_contents();
ob_end_clean();
$pagetitle = "My Home Page";
//Apply the template
include("master.php");
?>
Master Page
Code: Select all
<head>
<title><?php echo $pagetitle; ?></title>
<link rel="stylesheet" type="text/css" href="StyleSheets/newsuperstyle.css" />
</head>
<body>
<center>
<ul>
<li><a href="home.php">Menu Item 1</a></li>
<li><a hret="aboutme.php">Menu Item 2</a></li>
</ul>
<?php echo $pagemaincontent; ?>
</center>
</body>
</html>
Any further help would be much appreciated!
FYI: I'm using PHP5 on Mac with built in Apache Server.
Re: How to include javascript in a php page?
Posted: Sun Aug 24, 2008 11:20 pm
by gyclone
Ok, once I moved all the files to the production server, the include method worked. I'm not sure why it wouldn't work on my dev. server, but I can worry about that later.
I would, however, still like to know if it is possible to use JavaScript right in my php page, and if so, what the rules are for inclusion.
Thanks!
Re: How to include javascript in a php page?
Posted: Mon Aug 25, 2008 3:14 am
by Sindarin
You should be okay doing so, as long as you escape any quote characters. You need to echo it though, Javascript cannot have any effect server side.
Re: How to include javascript in a php page?
Posted: Mon Aug 25, 2008 4:09 am
by onion2k
All PHP does is make text*. This is a principle so many new users seem to have a problem grasping. If you want some JS in your page just echo it out in exactly the same way as you'd echo any other text. That might be with the echo function, or print, or with an include, or even breaking out of PHP and just dumping the text into the script like:
Code: Select all
<?php
//heres some PHP
echo "<span>Here's some HTML</span>";
echo "<script><!-- here's some javascript; //--></script>";
?>
<script>
<!--
//Here's some more Javascript;
//-->
</script>
<?php
//here's some more PHP
?>
All PHP does is manipulate text - that text is what the browser gets.
* And lots of other things like PDF files, CSV files, XML files, etc, but ultimately even those are just text too. Heck, even images are. But that's besides the point.
Re: How to include javascript in a php page?
Posted: Mon Aug 25, 2008 7:34 pm
by gyclone
Thank you all!!
One last question: Most of the other forums I post in have a way of marking a question as answered. I haven't found anyway of doing that here. Am I missing something, or do threads just get closed automatically at some point?
Thanks again!