Search code examples
javascriptphpundefined

Js Function not defined when I use php include


I used to write JS inline in my footer but since it's getting too much I want to write it in an external file and include the file in the footer then. I'm currently using

<?php include "https://example.com/myjs.php"; ?>

in the footer.

In that Php file which is mainly js with a few php expressions, I'm defining all the functions first and afterwards I'm calling some of them functions "onload" with

jQuery(document).ready(function() {
 // functions....  
 });

I've put everything inside

<script></script>

The thing is that I get an error showing the functions I'm calling arent defined. However, when I just paste the code in the footer template inside

<script> </script> 

tags it works fine.

I'm assuming that the error is with the tags and inlcude... What am I doing wrong?

EDIT: I am using WP functions which are based on logged in user id and page id. Do these might cause the errors? I'm using wordpress with jQuery.


Solution

  • I could solve the problem: I defined the php vars in JS before the script within the footer.php. Credits to: Access PHP var from external javascript file

    Then I just replaced the php vars wih the js vars and included it with html as script. Works like a charm now, thanks anyway for all the help!