The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End About Including Files .

  • This topic is empty.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
  • #171759

    Hey ,

    I am currently working in a project which have following files.

    …like that.

    ok for example header.php is included in about.php and main.php

    Header.php has got codes upto </head> Tag.

    Now my question is that how do i will include JavaScript file like <script src=”ind.js”….> in about.php and it should go before </head> which is located in header.php

    But i don’t want to to be shown in main.php.

    How do i do it…

    Sorry if the topic title sounds funny or wired as i could not find good one.

    Any solution ?


    It is for jquery and it uses $(document).ready. i just want to include the that jquery file in before </head> which is on header.php by writing it in about.php


    Using TheDoc’s example you can wrap your script in an if statement

    // header.php
      <?php if(basename($_SERVER['PHP_SELF']) != 'about.php'): ?>
      <script src="path/to/jQuery.js"></script>
      <?php endif; ?>

    The reasoning behind placing your script tags before the closing </body> tag is not to delay execution of the script but to avoid any delay in the rendering of your document whilst your scripts download.

    This might not matter for a couple of small scripts but may delay the user from viewing your page by a couple of seconds if you have many larger scripts.

    This is not always good practice though and for some scripts such as Modernizr it is better to have them placed in the <head> as it provides you with information which may be used to affect the rendering of the rest of your page.


    THanks but it not only about.php there are other like contact.php etc..then i should same thing again and again ??…


    If you’re conditionally including this file then you need to decide what your conditions are.

    If you want to include it only on a single page then you could use

    if(basename($_SERVER['PHP_SELF']) == 'whatever.php')

    else if you want to exclude it on a single page then

    if(basename($_SERVER['PHP_SELF']) != 'whatever.php')

    if you want to include it on a number of selected pages then I would check against an array

    $pages = array('whatever.php', 'page2.php', 'somepage.php');
    if(in_array(basename($_SERVER['PHP_SELF']), $pages))

    This is taking place in your header.php file, which I’m assuming is included at the top of each page, so you wouldn’t be repeating yourself.


    ok man but again suppose i want to show jquery in about.php inside head tag..and i want to show modernizer js in contact.php and i want to show colorpicker.js in index.php and i want to show formvalidater.js in post.php…. Now how do i do it?


    Well if that is the case, then using the above method, yes you would have to add a conditional statement for each of those.

    I realise this is probably a hypothetical situation, but why do you want to include your scripts so selectively?

    If you can explain the reasoning behind it people might be able to provide you with a more efficient solution than conditionally loading different JavaScript files per page.


    Ok But i have got such page around 50 …I use 50 conditionals that too messy….

    let me explain more

    for instance this is code in footer.php

    ~~~~&gt; I want to show the script #1, #2 , #3 etc.. here &lt;~~~~

    this is about.php

    &lt;script type="text/javascript" src="ajax-about.js"&gt;&lt;/script&gt;
    require ("footer.php");?&gt;

    this is index.php

    &lt;script type="text/javascript" src="modernizer.js"&gt;&lt;/script&gt;
    require ("footer.php")?&gt;


    i want to show this ajax-about.js and modernizer.js before the </body> tag which is located at footer.php …

    Now is there any solution.?


    Is there a reason that you can’t combine them into one file and load that on all pages?


    I want to show demos like color picker etc… and i want those script directly go on footer or in header..on footer it is no problem but in header i cannot figure out to show before </head> tag


    If your entire header is in one file, which you simply include, then there is no opportunity to affect its contents. Something like this might work for what you need:


    <!doctype html>
            <meta charset=utf-8>
            <title>include with variables</title>
            <?= isset( $__header_js )? $__header_js: "" ?>

    What you’re doing is checking if a particular var exists, and then printing it if it does. So, to add javascript to the <head>, you’d just need to do:


    $__header_js = '<script src="my.js"></script>
            <script src="my_other.js"></script>';
    include "path/to/header.php";
Viewing 11 posts - 1 through 11 (of 11 total)
  • The forum ‘Back End’ is closed to new topics and replies.