WordPress.org

Ready to get started?Download WordPress

Forums

How do I add an onload event for one specific page? (5 posts)

  1. rowan.bradley
    Member
    Posted 4 years ago #

    I need one specific page on my WordPress site to call a Javascript function when the page is loaded. I can't do this in the page itself, because there is no body tag to insert the onload event into. I can't do it in the header file because then it will affect all the other pages on the site.

    How do I do this?

    Thanks - Rowan

  2. brett.buchanan
    Member
    Posted 4 years ago #

    I'm also looking for an answer to this.

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    Use a page template and include an alternative header file inside that page template.

    eg.

    <?php get_header('withjs') ?>

    Would then include/call a file named header-withjs.php (assuming it exists).

    You can then do what you like inside your alternative header file (such as including special JS files, and so on).

    Related:
    http://codex.wordpress.org/Function_Reference/get_header
    http://codex.wordpress.org/Pages#Page_Templates

  4. demegeek
    Member
    Posted 3 years ago #

    How would you do it if you wanted it on all pages?

  5. Ben Hutchings
    Member
    Posted 3 years ago #

    @demegeek If you want it on all, you could just put your code in the normal header.php in the theme.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags