1

I have a php file containing the following line;

$num= "<script type='text/javascript' src='hn_includes/common.js'>boo();</script>";

and of course have a javascript file common.js which contains a function boo()

This is never called...

In an attempt to debug, I've placed a console.log("common.js loaded"); at the top of the common.js file and this tells me that it is indeed being loaded but the call to function boo() never seems to happen.

Additionally, if I simply change my line of code above to;

$num= "<script>boo();</script>";

and add boo() to top of the php file as below;

<script>
    console.log ("I'm here");
    function boo()
    {
        console.log("boo");
    }
</script>

I get both the "I'm here" and also the "boo".

Any suggestions on where I'm going wrong would be greatly appreciated...

plisken
  • 67
  • 1
  • 8
  • 1
    Load your script with ` – brombeer Mar 26 '21 at 09:45
  • 1
    See also: https://stackoverflow.com/questions/6528325/what-does-a-script-tag-with-src-and-content-mean – CBroe Mar 26 '21 at 09:49
  • so you are getting both "I'm here" and "boo" because "I'm here is a console log which will happen every time you run the page" if you want yo figure out that you can call a specific method create another method `boo()` in the same class and try call it you will find that you can call `boo` or `foo` as you want. Please share some more details if that is not what you are looking for – Mohamed Bdr Mar 26 '21 at 09:53

1 Answers1

0

You are only declaring variable in the PHP file. To make it valid accessible in page you need to echo it. like

$num= "<script type='text/javascript' src='hn_includes/common.js'>boo();</script>";
echo html_entity_decode($num);
M Umer Yasin
  • 284
  • 3
  • 6