The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

how to javascript on particular page not on all pages

  • # November 19, 2008 at 6:30 am

    on a website project i want to use some javaScript only on some pages not on whole project. if i include js into header.php then it will render every time on every page which i don’t want. what is the best and standard method to do this?

    i’m using this format

    content.php = index.php

    # November 19, 2008 at 7:38 am

    just make an include for the javascript in the header I’d say.

    So you’ll have:
    content.php and so on…

    and only include the JavaScript when you need it to be rendered

    # November 19, 2008 at 10:01 am

    You can do something like this just include it in your header.

    $pageUrl = $_SERVER[‘PHP_SELF’];
    $pageUrl = str_ireplace(“.php”, “”, $pageUrl);

    switch ($pageUrl) {
    case “page_name_1”: case “page_name_2”: case “page_name_3” :
    echo ‘‘;

    What this script dose is that it checks the page name of the currently requested file and if it matches the one you want it outputs the JavaScript code.

    # November 20, 2008 at 12:04 am

    This reply has been reported for inappropriate content.

    A firebug extension YSLOW suggest to put javascript in bottom of the page. but it is semantic or correct . as i know javascript should be placed in <head> only.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed