Home » Php » javascript – Running external JS within a PHP webpage

javascript – Running external JS within a PHP webpage

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have created a PHP version of my webpage, and it finds my external css just fine. However, I cannot get it to load the external JS code and I can’t figure out why that is. I am using WinSCP to connect to a server.

I know that PHP is server-side, and technically, the JS is also on the server right? In my code, I link the css down to the root folder, and it comes back in the PHP version of the webpage. But JS won’t. I have searched for quite some time on how to make this work, and perhaps it doesn’t.

<?php
 $path = $_SERVER['DOCUMENT_ROOT'];
 $header= $path."/includes/header.php";
 include ($header);
 $menu= $path."/includes/menu.php";
 include ($menu);
 $footer= $path."/includes/footer.php";
 include ($footer);

?>
<html>
    <head>
        <title>My Profile</title>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="../css/mycss.css" rel="stylesheet" type="text/css"/>
        <script src="../js/myjs.js" type="text/javascript"></script>

    </head>

How to&Answers:

It should be working. Load the php script in a browser. Right click, Inspectand go to Console. (But I guess you should know how to go to the console already.)

If it mention anything about a 404, the file doesn’t exist or you’re not putting the correct url in the src="".

If it’s not, please comment the error in the console. If there isn’t any, the script was loaded, but you don’t realize it somehow.

Edit: Also, maybe your mime type is wrong, if the above doesn’t work, try replacing type="text/javascript" to type="application/javascript".