Forum Moderators: open

Message Too Old, No Replies

calling external javascript with onclick

object expected

         

rdunne

3:58 pm on Apr 22, 2007 (gmt 0)

10+ Year Member



I have a php webpage which is calling a javascript function in an external js file using onclick. If I put the javascript with the same file it executes fine. But if I put it in an external javascript file I get object expected. I have the external javascript file in an include with the head. I jhave the full path within the include.
extract from php file

<script src="http://localhost/Databases/webpages/Hi.php">
</script>

<form action="Hi.php">
<input type="button" value="Hi" onclick="Hi();" />
</form>

Hi.js


<script type="text/javascript" language="javascript">
function Hi()
{
alert("hi");
}
</script>

Can someone explain why this is not working and a possible solution?

rdunne

Dabrowski

6:58 pm on Apr 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure what you mean by this:

<script src="http://localhost/Databases/webpages/Hi.php"> </script>

An external javascript should contain ONLY Javascript, i.e. what goes between your <script> tags in HTML normally.

Why are you calling a PHP script in your JS include line?

penders

9:09 pm on Apr 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The external JS file should contain just the JS - there should be *NO* <script> elements inside your external JS file. The <script> element stays in your HTML file.

Why are you calling a PHP script in your JS include line?

The PHP script (server-side) may output JavaScript and if PHP is sending the correct header, a standard JS file will appear at the browser end. Either that or it's a typo?! ;)

Fotiman

9:20 pm on Apr 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Looks to me like your script element is pointing at hi.php, when your JavaScript file is actually hi.js.

Dabrowski

9:59 pm on Apr 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would say this is definately the problem, as in the original post he uses
<form action='Hi.php'>
.

Hi.php can't be both at once!