ThemeShaper Forums » Thematic

[closed]

Adding an 'onload' to the body tag?

(10 posts)
  • Started 13 years ago by manlikemalcolm
  • Latest reply from helgatheviking
  • This topic is resolved
  1. I would like to add the followng code to the body tag

    <body onload="onLoad();" onresize="onResize();">

    on a specific page.

    Thanks, Malcolm

    Posted 13 years ago #
  2. Note: Read on... A better solution is listed two posts down.

    Malcom -

    Not really a straight forward solution here. One way to do this is to copy the header.php from thematic to your child theme directory and make the changes in there. Only problem with that is that if future thematic releases revise the header.php you'll need to copy it and edit all over again which kind of defeats the benefits of the theme framework.

    There is a hook in the thematic_body_class function but since the function exists within the quotes of the class attribute and the classes are dynamically generated theres no way I know of to hook in and add your bit onto the end unless you rebuild a child_body_class and then tack the onload and onresize onto the end.

    function add_body_onload() {
    $onload = '" onload="onLoad();" onresize="onResize();';
    echo (child_body_class() . $onload) ;
    }
    add_filter('body_class','add_body_onload');

    and then build your child_body_class function to output the classes you desire.

    Posted 13 years ago #
  3. Gene,

    Is this still the only way to accomplish adding an onload event to the body?

    Thanks!

    Posted 13 years ago #
  4. Thanks for digging this up. I'm not sure why I wasn't seeing this before. It's actually really simple to do this by filtering the body class. Oh well, live and learn.

    function add_body_onload($c) {
    	$c[] = '" onload="onLoad();" onresize="onResize();';
    	return $c;
    }
    add_filter('body_class','add_body_onload');

    and to adress Malcom's desire to do this for a specific page you could use a conditional tag to make this work only on a specific page or template.

    -Gene

    Posted 13 years ago #
  5. thanks for this. i had tracked down that $c variable and was wondering how to filter it w/o filtering the entire function.

    function add_body_onload($c) {
    if (is_page_template('templatename.php')){
    	$c[] = '" onload="FunctionName();';
    	return $c;
    
    }
    else {
    return $c;
    }
    }
    add_filter('body_class','add_body_onload');

    in case anybody needs it in the future, here is what i did to make it conditional based on a page template. i found that if i didn't use return $c for the else I ended up w/ no body classes.

    Posted 13 years ago #
  6. Awesome! Thank you, em hr and helgatheviking! :)

    Posted 13 years ago #
  7. thank you for the guide. How to add to the admin body class???

    Posted 12 years ago #
  8. i don't know that you can w/ thematic. you might be able to filter a default WP function to accomplish it, but i don't know the function of the top of my head. you'll have to scour the WP code. why would you want to?

    Posted 12 years ago #
  9. I apologize if this is been dead and I'm missing something. When you use the BETTER code posted above:

    function add_body_onload() {
    $onload = '" onload="onLoad();" onresize="onResize();';
    echo (child_body_class() . $onload) ;
    }
    add_filter('body_class','add_body_onload');

    It ends up splitting the class attribute of the body tag like this:

    <body class="home blog " onload="HereIsMyFunction();" windows firefox ff4">

    You end up with some of the class names just floating in your body tag? Am I missing something or is there a way to have your onload code placed at the END of the $c[] array?

    Posted 11 years ago #
  10. you're right, the classes seem to get split. make sure you are using version 0.9.8

    booyah

    function childtheme_override_body(){
        echo '<body onload="onLoad();" onresize="onResize(); class="';
        thematic_body_class();
      	echo '">' . "\n\n";
    }
    Posted 11 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.