Global Javascript (4 posts)

  1. rsvp2u
    Posted 7 years ago #

    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?

  2. Darran
    Posted 7 years ago #

    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.

  3. rsvp
    Posted 7 years ago #

    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!

  4. 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

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.