1

I'm creating a blog where I included header.php(Header, included in every page). Header contains a section where all the scripts (Scripts that will need in every pages) will be included.

header.php

<!DOCTYPE html>
  <html>
    <head>
      <!-- Scripts Section -->
      <script src="example01.js"></script>
      <script src="example02.js"></script>
      <title>Demo</title>
    </head>
    <body>
      <p>Blog</p>

But in a specific page, I've to include a specific script (file.js named).

index.php

<?php
  include('header.php');
  importjs('file.js'); /* Any Function To Import JavaScript*/
?>

If I include the JavaScript file here then it'll be included in body not the scripts section in the header. I don't know how to create that function. So please tell me how to create that function. create function in header.php and please first include the header before using function.

Thank You

7
  • Are all the javascript files that you include in the header.php script hardcoded or dynamically generated somehow? Commented Sep 28, 2022 at 6:53
  • 1
    simply remove importjs('file.js'); and add <script src="file.js"></script> after the line ?> in this particular php file (Note: you can have multiple <script>....</script> in a single page) Commented Sep 28, 2022 at 6:55
  • 1
    It doesn't matter if the script is in the body or the head, it makes no difference in practice Commented Sep 28, 2022 at 6:59
  • 1
    Why don't you use the function approach that you had in your last question? It works much better... see my updated answer to your old question Commented Sep 28, 2022 at 7:24
  • 1
    I don't think anyone would be perturbed to find a script tag in the body, it's not at all unusual. But you can also use Nick's idea, it's a good one. Commented Sep 28, 2022 at 10:03

1 Answer 1

1

I think, you should to write your line code like this example:

<?php int a; ?> 
<script type="text/javascript" src="file.js" /> 
<?php int b; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

What is it, actually?
it similar with Ken Lee comment. We need to separate the <script /> line from the <?php ?> Because of <script /> doesn't work in the <?php ?> line. Thank you

Your Answer

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