Forums

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
    Posts
  • #171759

    Hey ,

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

    header.php
    main.php
    about.php
    footer.php
    …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 ?

    #171893

    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

    #171899
    Mawaha
    Participant

    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; ?>
    </head>
    

    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.

    #172061

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

    #172066
    Mawaha
    Participant

    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.

    #172067

    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?

    #172070
    Mawaha
    Participant

    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.

    #172072

    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

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

    this is about.php

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

    this is index.php

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

    require(“footer.php”)

    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.?

    #172080
    Mawaha
    Participant

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

    #172111

    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

    #172148
    __
    Participant

    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:

    header.php

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

    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:

    your_page.php

    <?php
    
    $__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.