WordPress.org

Support

Support » How-To and Troubleshooting » Global Javascript

Global Javascript

  • Hi I have a little bit of javascript i want to run on my blog each time the user clicks on something. Is there a way to set it as some type of “global” action somewhere or a plugin maybe that does this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Define it as a function.

    Loop through the entire document for instances of a link.

    And modify the onClick event to run this function every time.

    I have been so used to making use of MooTools to do it that I have forgotten how to do it using native JavaScript codes.

    well sounds reasonable but a little beyond my skills…

    maybe some insights on where <what file> to do this?

    or how it might be easier to accomplish with mootools?

    thanks for any hep here!

    In the theme’s header.php file, in the head section, before the wp_head() call, add something like this:

    <?php wp_enqueue_script('jquery'); ?>
    <script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery('a').click(function() {
            // put the code you want to add to all the onclicks here
            // "this" refers to the anchor tag, like this.href gets you
            // the location the link goes to.
            // make your code return true to let the click happen
            // return false to stop the click
        }
    });
    </script>

    Simple as that. If you want to only add it to some links, then you can change the ‘a’ to be a bit more specific.. Like, say, all the links inside the div with a class of “post” would be ‘.post a’ instead.

    jQuery. It’s extremely powerful.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Global Javascript’ is closed to new replies.
Skip to toolbar