- This topic is empty.
-
AuthorPosts
-
June 4, 2014 at 3:44 am #171759PHP Strome the FrontParticipant
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 ?
June 4, 2014 at 9:44 pm #171893PHP Strome the FrontParticipantIt 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
June 5, 2014 at 12:57 am #171899MawahaParticipantUsing 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.June 6, 2014 at 5:02 am #172061PHP Strome the FrontParticipantTHanks but it not only about.php there are other like contact.php etc..then i should same thing again and again ??…
June 6, 2014 at 6:53 am #172066MawahaParticipantIf 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.June 6, 2014 at 7:25 am #172067PHP Strome the FrontParticipantok 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?
June 6, 2014 at 8:57 am #172070MawahaParticipantWell 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.
June 6, 2014 at 10:02 am #172072PHP Strome the FrontParticipantOk 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
<footer> </footer> ~~~~> I want to show the script #1, #2 , #3 etc.. here <~~~~ </body> </html>
this is about.php
<script type="text/javascript" src="ajax-about.js"></script> <? require ("footer.php");?>
this is index.php
<script type="text/javascript" src="modernizer.js"></script> <? require ("footer.php")?>
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.?
June 6, 2014 at 11:43 am #172080MawahaParticipantIs there a reason that you can’t combine them into one file and load that on all pages?
June 6, 2014 at 8:44 pm #172111PHP Strome the FrontParticipantI 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
June 7, 2014 at 1:25 pm #172148__ParticipantIf 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";
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.