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

VLAZ
  • 26,331
  • 9
  • 49
  • 67
  • Are all the javascript files that you include in the `header.php` script hardcoded or dynamically generated somehow? – Professor Abronsius Sep 28 '22 at 06:53
  • 1
    simply remove `importjs('file.js');` and add `` after the line ?> in this particular php file (Note: you can have multiple in a single page) – Ken Lee Sep 28 '22 at 06:55
  • 1
    It doesn't matter if the script is in the body or the head, it makes no difference in practice – ADyson Sep 28 '22 at 06:59
  • Leave the ending `head` tag out of the header.php file, and add it on every page file instead. That way you can add lines to the head section. – Teemu Sep 28 '22 at 07:14
  • 1
    Why don't you use the function approach that you had in your last question? It works much better... see my [updated answer](https://stackoverflow.com/a/73875887/9473764) to your old question – Nick Sep 28 '22 at 07:24
  • @ADyson, I want to create a professional website. So I want it to be clean and simple code. Because whenever someone see my site's source code it will look pretty –  Sep 28 '22 at 09:53
  • 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. – ADyson Sep 28 '22 at 10:03

1 Answers1

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; ?>
Ivar
  • 6,138
  • 12
  • 49
  • 61