WordPress.org

Ready to get started?Download WordPress

Forums

js in header on certain page only (5 posts)

  1. JayK
    Member
    Posted 9 years ago #

    Hi,
    I am trying include a javascript file in my header.php file, but for a certain page only (a page with the slug 'band').

    I have roughed out the code (my php isn't too hot), but it is not working correctly, where am I going wrong?
    <?php if (is_page('band')) {
    echo "<script language="JavaScript" type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/ttca.js"></script>";
    } ?>

    Many thanks,
    Jason

  2. eridan
    Member
    Posted 9 years ago #

    you have to escape the characters. For example, echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"...\">

  3. ragecg
    Member
    Posted 9 years ago #

    Just wondering if this worked for you JayK, as I have the same problem, and I am going to try this this weekend:)

  4. ragecg
    Member
    Posted 9 years ago #

    Dam. It didn't work.

    Are there any plugins for this?

    Just wondering:)

  5. First off, you don't do <?php ?> when you're already inside PHP. Second, it's easier to do one of these two.

    Using single quotes for the echo so that you don't have to escape the double quotes and using the . joiner or whatever it's called:

    <?php if (is_page('band')) {

    echo '<script language="JavaScript" type="text/javascript" src="' . bloginfo('stylesheet_directory') . '/ttca.js"></script>';

    } ?>

    Or you can just step out of PHP:

    <?php if (is_page('band')) : ?>

    <script language="JavaScript" type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/ttca.js"></script>

    <?php endif; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic