0

I have a function named

test() 

defined in a javaScript file called test.js which is reference from the following html:

<html>
<head>
    <script src="test.js"></script>
</head>
    <?php 
        echo "<script>test();</script>"; 
     ?>

<body>
</body>
</html>

I have verified that test.js works on it's own and the other javascript is being executed by the php. But I'm getting an undefined error.

Error: test() undefined

Can any one tell my how to properly reference a external javascript file from a php script?

2
  • is test.js in the same location as your php file? Commented Mar 17, 2014 at 6:37
  • no, the actual extension is "js/test.js". I omitted the "js/" in the example for simplicity. Commented Mar 17, 2014 at 6:41

5 Answers 5

1

put it in your body and test it again

Sign up to request clarification or add additional context in comments.

1 Comment

This didn't change anything
0

Try like this

<html>
<head>
    <script src="test.js"></script>
</head>


<body>
    <?php 
        echo "<tag onClick='test()'>text text text </tag>"; 
     ?>
</body>
</html>

Comments

0

1- run the function manualy not with php : do some thing like :

<body onload ="alert('test');test();">

if worked test it:

<body onload ="alert('test');<?php echo "test();";?>">

if not worked your javascript function is not ok.

1 Comment

Why two answers? You could have edited the first one.
0

It should just work, have you really tried it like this? :

<html>
    <head>
        <script src="js/test.js"></script>
    </head>
    <body>
        <?php
            echo "<script>test();</script>"; 
        ?>
    </body>
</html>

Comments

0

You can try out this:

echo '<script type="text/javascript">','your_function();','</script>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.