Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: Ajax Tabs Content Script - conflict with countdown script?

  1. #1
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Ajax Tabs Content Script - conflict with countdown script?

    1) Script Title: Ajax Tabs Content Script (v 2.2)

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...axtabscontent/

    3) Describe problem: I am using Keith Wood's jQuery countdown script to display timers in the div. So far, the countdown timers work perfectly when fetched through an external page via iframe or just display as inline content. But the timers do not show up when fetched through an external page via Ajax. Is there a way to resolve this? Thanks~

    Countdown script URL: http://keith-wood.name/countdown.html

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Ordinarily javascript on an AJAX imported page doesn't run. However, if you go to:

    http://www.dynamicdrive.com/dynamici...uppliment3.htm

    and scroll down a bit you will find a section called Main script function and methods syntax with a table called 'ddajaxtabs() constructor function and methods'. The very last entry in that table is:

    Code:
    instance.onajaxpageload=function(pageurl){
     //custom code here
     }
    Its description is:

    A custom event handler you can use to execute your own code whenever a tab with content fetched via Ajax is clicked on. See the section on "Nesting Ajax Tabs" for more info.
    It can be used to run your countdown code. As it says at that quoted link:

    the code to invoke it needs to be moved to the main page instead and called via the "onajaxpageload" event handler
    That means that your style and external script tags for jQuery Countdown, example:

    Code:
    <style type="text/css">
    @import "css/jquery.countdown.css";
    
    #defaultCountdown { width: 240px; height: 45px; }
    </style>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery.countdown.js"></script>
    need to be in the head of the 'top' page, the page with Ajax Tabs on it.

    Once you have that - say your tab instance is like:

    Code:
    <script type="text/javascript">
    
     var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
     countries.setpersist(true)
     countries.setselectedClassTarget("link") //"link" or "linkparent"
     countries.init()
    
     </script>
    You could add the highlighted as shown:

    Code:
    <script type="text/javascript">
    
     var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
     countries.setpersist(true)
     countries.setselectedClassTarget("link") //"link" or "linkparent"
     countries.init()
    
     countries.onajaxpageload=function(pageurl){
    	if (pageurl.indexOf("externalcountdown.htm")!== -1){
    		var newYear = new Date(); 
    		newYear = new Date(newYear.getFullYear() + 1, 1 - 1, 1); 
    		$('#defaultCountdown').countdown({until: newYear});
    	}
     }
    
     </script>
    where externalcountdown.htm is the name of the page with the defaultCountdown span or div on it.

    If you want more help:

    Please post a link to a page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    What I have been trying to accomplish is to retrieve a series of date/time from database, compare them with current date/time and start counting down.


    test URL : http://test.insurancesg.com/view-sales

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The way you have it is fine and by far is easier than using AJAX for this kind of thing. Is it just that you have some aversion to iframe? If you want to switch to AJAX and continue using Ajax Tabs (jQuery has its own AJAX functions, these might be able to pull in your scripts and execute them see - http://api.jquery.com/load/ and http://api.jquery.com/jQuery.ajax/, but that will still be much more complex than iframe), you will probably have to do it as outlined in my previous post.

    Please keep in mind that AJAX doesn't import the page, only the content on the page. Most scripts and styles are ignored.

    One good thing about jQuery though generally, if you do something like:

    Code:
    $("#ks0").whatever()
    and there is no element with that id, nothing will be done and there will generally be no error.

    But basically what you would need to do is pull the info from your database to the view-sales page to here (addition highlighted):

    Code:
    		<ul id="countrytabs" class="shadetabs">
    		<li><a href="http://test.insurancesg.com/wp-content/plugins/KiasuSales/ending.php" rel="countrycontainer" class="selected">Ending</a></li>
    		<li><a href="http://test.insurancesg.com/wp-content/plugins/KiasuSales/starting.php" rel="#iframe">Starting</a></li>
    		<li><a href="http://test.insurancesg.com/wp-content/plugins/KiasuSales/expired.php" rel="countrycontainer">Expired</a></li>
    		</ul>
    
    		<div id="countrydivcontainer" style="border:1px solid gray; width:97%; margin-bottom: 1em; padding: 5px"></div>
    
    		<script type="text/javascript">
    			var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
    			countries.setpersist(true)
    			countries.setselectedClassTarget("link") //"link" or "linkparent"
    			countries.init()
    			
    			 countries.onajaxpageload=function(pageurl){
    				if (pageurl.indexOf("starting.php")!== -1){
    					$("#ks0").countdown({until: new Date("2011/05/13 22:03:00"), onExpiry: liftOff})
    					function liftOff() { 
    						window.location.href="http://test.insurancesg.com/90/test-time-2";
    					}
    				}
    			 }
    		</script>
    Part or all of the red would come from the database. That depends upon what you are currently getting from the database on starting.php. Since it's no longer iframe though, window.location.href = would need to be changed to some sort of AJAX call:

    Code:
    $('#countrydivcontainer').load('http://test.insurancesg.com/90/test-time-2');
    The jQuery .load() function also allows for an optional callback if you need to initialize timers on the page it imports (the countries.onajaxpageload function is the optional callback for Ajax Tabs).

    You could have else if statements to follow for other pages. Possible example of one:

    Code:
    <script type="text/javascript">
      var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
      countries.setpersist(true)
      countries.setselectedClassTarget("link") //"link" or "linkparent"
      countries.init()
    
       countries.onajaxpageload=function(pageurl){
        if (pageurl.indexOf("starting.php")!== -1){
          $("#ks0").countdown({until: new Date("2011/05/13 22:03:00"), onExpiry: liftOff})
          function liftOff() { 
            $('#countrydivcontainer').load('http://test.insurancesg.com/90/test-time-2', function(){/* optional callback code */});
          }
        } else if (pageurl.indexOf("some_other.php")!== -1){
          $("#ks0").countdown({until: new Date("2011/06/19 10:30:00"), onExpiry: liftOff})
          function liftOff() { 
            $('#countrydivcontainer').load('yet_another_page', function(){/* optional callback code */});
          }
        }
       }
    </script>
    These scripts and style and any others required by AJAX imported pages should be on the view-sales page (the 'top' page):

    Code:
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
    <script type="text/javascript" src="http://test.insurancesg.com/wp-content/plugins/KiasuSales/js/jquery.countdown.js"></script> 
    <style type="text/css"> 
    @import "http://test.insurancesg.com/wp-content/plugins/KiasuSales/css/jquery.countdown.css"; 
    #ks0 { width: 240px; height: 45px; }
    </style>
    But, as I say, iframe is so much easier, why not just use it?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Basically this is what I have in the starting.php
    PHP Code:
    <?php global $wpdb,$ksdb,$root_dir;
        
    $now=date('Y-m-d H:i:s');
        
    $wpdb->show_errors();
        
    $ks_view $wpdb->get_results("SELECT * FROM $ksdb WHERE (start_date>'$now' && exp_date>'$now') ORDER BY start_date ASC");
        
    $i=0;
    ?>
    <!-- #Countdown Timer starts -->
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 
    <script type="text/javascript" src="<?php echo $root_dir.'/wp-content/plugins/KiasuSales/js/jquery.countdown.js';?>"></script> 
    <!-- #Countdown Timer End-->
    <style type="text/css"> 
    @import "<?php echo $root_dir.'/wp-content/plugins/KiasuSales/css/jquery.countdown.css';?>"; 
    <?php for ($rows=0;$rows<count($ks_view);$rows++){?>
    #ks<?php echo $rows;?> { width: 240px; height: 45px; }
    <?php }?>
    </style>
    <?php    foreach($ks_view as $ks) { 
                
    $post=get_post(intval($ks->postid));
                
    $author get_userdata(intval($post->post_author));
                echo 
    "<a class='tabtitle' href=".get_permalink(intval($ks->postid)).">".$post->post_title."</a> by ".$author->display_name."<br>";
                
    $start_date=preg_replace('/-/','/',$ks->start_date);
                echo 
    "Sales starting in";
            
    ?>
            <script type="text/javascript">
                $(function () {
                    $("#ks<?php echo $i;?>").countdown({until: new Date("<?php echo $start_date;?>"), onExpiry: liftOff})
                });
                function liftOff() { 
                    window.location.href="<?php the_permalink() ?>";
                }
            </script>
            <div id="ks<?php echo $i;?>"></div><br>
    <?php    $i++; }?>
    Due to some previous bad experiences with iframe, I would rather not use it. However, since I'm not familiar with jQuery, it seems to be the easier way out for me. Nonetheless, I will keep on trying until I run out of all possible options, before switching to iframe~ Thanks~

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    My PHP is probably just marginally if at all better than your jQuery. I have no experience with databases.

    If <?php the_permalink() ?> is different for each countdown (looks like it is from the served source code of ending.php), I can see one problem right away. Even with starting.php as is, as a standalone page, if there are more than one countdowns on it, the last:

    PHP Code:
                function liftOff() {  
                    window.location.href="<?php the_permalink() ?>"; 
                }
    will overwrite all the others, so any/all countdowns, once they expire, would go to the last one's 'the_permalink()'. To fix that, you would either need to establish javascript scope for the onExpiry function, or come up with unique names (as you have for the divisions' ids) for each liftOff function.

    I'd go with javascript scope, change:

    PHP Code:
            <script type="text/javascript"> 
                $(function () { 
                    $("#ks<?php echo $i;?>").countdown({until: new Date("<?php echo $start_date;?>"), onExpiry: liftOff}) 
                }); 
                function liftOff() {  
                    window.location.href="<?php the_permalink() ?>"; 
                } 
            </script>
    to:

    PHP Code:
            <script type="text/javascript"> 
                $(function () { 
                    $("#ks<?php echo $i;?>").countdown({until: new Date("<?php echo $start_date;?>"), onExpiry: function(){  
                        window.location.href="<?php the_permalink() ?>"; 
                    }}) 
                });  
            </script>
    That still doesn't get us much closer to using the page with AJAX though.

    For that, do as I suggest. If you want starting.php to still work on its own, you can leave it (with the one change I suggested above) alone. Even if you don't want it to work on its own, it would still need that portion of the PHP code that's required to generate the markup. Either way, put this:

    PHP Code:
    <?php global $wpdb,$ksdb,$root_dir
        
    $now=date('Y-m-d H:i:s'); 
        
    $wpdb->show_errors(); 
        
    $ks_view $wpdb->get_results("SELECT * FROM $ksdb WHERE (start_date>'$now' && exp_date>'$now') ORDER BY start_date ASC"); 
        
    $i=0
    ?> 
    <!-- #Countdown Timer starts --> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>  
    <script type="text/javascript" src="<?php echo $root_dir.'/wp-content/plugins/KiasuSales/js/jquery.countdown.js';?>"></script>  
    <!-- #Countdown Timer End--> 
    <style type="text/css">  
    @import "<?php echo $root_dir.'/wp-content/plugins/KiasuSales/css/jquery.countdown.css';?>";  
    <?php for ($rows=0;$rows<count($ks_view);$rows++){?> 
    #ks<?php echo $rows;?> { width: 240px; height: 45px; } 
    <?php }?> 
    </style>
    on the top page (view-sales). And on that same page, where you have:

    Code:
    		<script type="text/javascript">
    			var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
    			countries.setpersist(true)
    			countries.setselectedClassTarget("link") //"link" or "linkparent"
    			countries.init()
    		</script>
    do:

    PHP Code:
    <script type="text/javascript">
        var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
        countries.setpersist(true)
        countries.setselectedClassTarget("link") //"link" or "linkparent"
        countries.init()
        countries.onajaxpageload=function(pageurl){
            if (pageurl.indexOf("starting.php")!== -1){
            <?php    foreach($ks_view as $ks) {
                    
    $start_date=preg_replace('/-/','/',$ks->start_date);
                    
    ?>
                $("#ks<?php echo $i;?>").countdown({
                    until: new Date("<?php echo $start_date;?>"),
                    onExpiry: function(){  
                                $('#countrydivcontainer').load('<?php the_permalink() ?>', function(){/* optional callback code */});
                            }
                        });
                <?php    $i++; }?>
            }
        }
    </script>
    Note: I've gone over this pretty thoroughly but have no way of testing it. Much of it relies upon the fact that your current code appears to work. It doesn't take into account things for which you might not have tested your current code for. And it may be subject to typos and/or misunderstanding on my part. You can backup what you have and try it, if there are PHP errors, those should be reported in some way, hopefully one that will be easy to understand and correct. If there are javascript problems, give me a link to the new page and I'll see what I can find out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi John, I tried your solutions but unfortunately, the outcome is still the same.

    Maybe I should just dropped the idea of using ajax and external files. Instead, I should have focus on using this tab content script(http://www.dynamicdrive.com/dynamici...tabcontent.htm) and find a way to refresh the tab contents whenever a tab is clicked~

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by nitroblood View Post
    Hi John, I tried your solutions but unfortunately, the outcome is still the same.
    Post a link to the example where you've tried what I suggested in my last post. It may just need some tweaking.

    Quote Originally Posted by nitroblood View Post
    Maybe I should just dropped the idea of using ajax and external files. Instead, I should have focus on using this tab content script(http://www.dynamicdrive.com/dynamici...tabcontent.htm) and find a way to refresh the tab contents whenever a tab is clicked~
    Unless you're willing to refresh the page each time a tab is clicked, that way is AJAX or iframe. So your stuck with the same issue again.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hi John,

    The URL is still http://test.insurancesg.com/view-sales. I amended it for the purpose of trying out tab contents script and I guessed you are right, I added 'location.reload()' to the js file and it refreshes the whole page.

    this.tabs[i].onclick=function(){
    tabinstance.expandtab(this)
    location.reload()
    tabinstance.cancelautorun() //stop auto cycling of tabs (if running)
    return false
    }


    By the way, you had also pointed out one of my mistakes. I didn't realised that the permalink had changed, it is suppose to return to the view-sales page whenever a countdown timer reaches 0, acting like a refresh.

    Will focus on ending.php since it has more time counters with similar functions and codes.

  10. #10
    Join Date
    May 2011
    Posts
    16
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Ok John, it seems that the declaration for the timers width & height disappear when I do a re-test on your suggestion. That should be the reason why I didn't see the timers during the first run.

    I managed to cut and paste from my backup and your script and voila! It works!

    However, new problem arises soon after:

    When a timer reaches 0, the content refreshes and all the timers disappear again~ I have to click on the 'view sales' again or refreshes the whole page to get them back, clicking on the 'ending' tab does NOT help at all. I checked the source code and everything seems to be intact~
    Last edited by nitroblood; 05-12-2011 at 07:37 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •