samurai_jane
samurai_jane

Reputation: 835

Need to fire an alert after "page load" on same page

This is a WordPress site and I have a template that renders all posts in a list at this URL http://www.example.com/articles. When the user clicks on a post title, it loads the content of the post using the same template but with a different URL http://www.example.com/articles/?article_id=298. What I cannot figure out is how to show an alert after the page loads the post content. I've tried setTimeout, $(document).ready, $(window).load, window.onload=funtion(), and now $(document).on("pagecontainerload",function().

JQuery/JavaScript

    $('a.open-alert').click(function() {
      $(document).on("pagecontainerload",function(){
          window.alert('hey');
      });
    });

HTML

            <?php
                $tearsheet = get_query_var('article_id');
                if($tearsheet != '') {
                    echo 'there is something here';
                } else {

                    $args = array(
                    'post_type' => 'post',
                    'posts_per_page' => -1,
                    );

                    $query = new WP_Query( $args );

                    if($query->have_posts()) :
                        while($query->have_posts()) :
                            $query->the_post();
                ?>
                            <div>
                                <?php $id = get_the_ID(); ?>
                                <?php $title = get_the_title(); ?>
                                <a class="open-alert" href="<?php bloginfo('url'); echo '/find-articles/?article_id='.$id; ?>"><?php echo $title; ?></a>
                            </div>
                <?php
                        endwhile;
                    endif;
                }
            ?>

Upvotes: 1

Views: 510

Answers (1)

Axel Amthor
Axel Amthor

Reputation: 11096

The click handler will never be executed, since the page unloads and a new page is loaded - the one in the link <a class="open-alert" href="<?php bloginfo.... You may have a return false in the onclick handler, but that would mean, the link will not be executed.

In order to do this dynamically, consider reloading only the container within that page, containing the post text by some Ajax functionality.

Upvotes: 2

Related Questions