Posts Tagged ‘JavaScript’

AJAX Calendar Loader Using jQuery July 15th, 2009

Michael Hartmayer

My good pal Austin needed help turning his static calendar into one that could change the month, using AJAX. The calendar itself was written in PHP. Here is the quick and easy solution I came up with for him, using jQuery:

$(document).ready(function() {
 
   // Create Date Object
   var jsDateObject = new Date();
 
   // Create Calendar Object ( Holds all your variables, etc )
   var jsCalendar = {};
       jsCalendar.currentMonth = jsDateObject.getMonth();
 
   // Controls the NEXT button
   $("#jsCalendarNext").click(function() {
      // Update Active Month
      jsCalendar.currentMonth++;
         if(jsCalendar.currentMonth>12) { jsCalendar.currentMonth=1; }
 
      // Get New Calendar
      ajaxUpdateCalendar();
   });
 
   // Controls the PREV button
   $("#jsCalendarPrev").click(function() {
      // Update Active Month
      jsCalendar.currentMonth--;
         if(jsCalendar.currentMonth<1) { jsCalendar.currentMonth=12; }
 
      // Get New Calendar
      ajaxUpdateCalendar();
   });
 
   // Use AJAX to update the Calendar
   function ajaxUpdateCalendar() {
      $.post(
         "phpCalendarLoader.php",
         { calendarMonth: jsCalendar.currentMonth },
         function(data) {
            $("#jsCalendarContainer").html(data);
         },
         "html"
      );
   }
 
});

From here, all Austin had to do was set his $month variable to $_POST['calendarMonth'];.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon

Continue reading...


 

PHP LoadScript Class June 27th, 2009

Michael Hartmayer

This is a really handy class I wrote that loads all your scripts Server Side so that when your page loads there aren’t so many HTTP Requests. Useful if you’re trying to load 6 different js files and 4 different style sheets, or whatever. That’s 10 requests, completely gone. Just keep in mind that it dumps all your files straight into the html document- so, as long as you don’t have a problem with that, this is a great tool.

In addition, it’s really easy to add your own script types. Check out the script for details.

( Available At: PasteBin: LoadScript Class )

Usage:

<?php
   # Include Class
   include_once('class.scriptloader.php');
 
   # Instantiate
   $jsLoader = new ScriptLoader('JS');
 
   # Add Scripts
   $jsLoader->addScript('my-javascript.js');
   $jsLoader->addScript('jquery.js');
   $jsLoader->addScript('other-scripts.js');
 
   # Dump into Document
   $jsLoader->echoScripts();
?>

On a quick note, it’d probably be a lot ‘neater’ to mod-rewrite a php file to handle at least one request per script type. That way it doesn’t all get dumped straight into the page.

Something like:

RewriteRule ^script\.(.*)$ script-loader.php?type=$1 [L]

And for script-loader.php

<?php
   switch($_GET['type']) {
      case 'js':
         // Load all of the JavaScript files here
         break;
   }
?>

That way when you load something like script.js it will dump all of your java scripts into a ‘fake’ file. Just make sure to use the default $ScriptLoader->type (don’t set one when instantiating) if you are loading it from a src=”foo.bar”.

.. or something like that xD

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon

Continue reading...