Displaying the AJAX content in a separate window

Write the following piece of code in your call back function to display the AJAX content in a separate browser window.
function(data) {
var myWindow = window.open('','Name of the Window','');

myWindow.document.open();
myWindow.document.write(data);
myWindow.document.close();
}

Note: You need to make sure your browser pop up blocker has disabled.

CakePHP: remove cakePHP Flash messages using JQuery

Typically you would have all the cakePHP flash messages inside the div - alert_messages.

By using JQuery, we can remove these after intended time period.
The following example removes the cakePHP flash message after 5 seconds time period.

$(document).ready(function() {
  $('#alert_messages').animate({opacity: 1.0}, 5000).fadeOut();  
});

style.display=‘none’ doesnt work on option tags in Chrome, IE

We cannot set a style, display:none on OPTION TAG in Chrome and IE, It can be done in Firefox !

Kinda Solutions :

1. Use disabled attribute for the OPTION

2. If you want to remove them permanently, use removeChild to delete the option.

3. If you want them to show/hide, then u might have to save the removed options into a javascript variable.

jQuery: Select all options in a Multiple-option Select Box

The following code will programmatically select all the options in a multiple-option Select box. Assuming you have jQuery setup.
$("#selectAllButt").click(function() {
  $("#mySelectList option").each(function() {
    $(this).attr("selected","selected");
  });
});

The following code will programmatically UN-Select all the options in a multiple-option Select box.
$("#selectNoneButt").click(function() {
  $("#mySelectList option").each(function() {
    $(this).removeAttr("selected");
  });
});

jQuery: Get checked checkboxes, radio buttons using Pseudo selectors

jQuery Pseudo Selectors are very efficient way to get the checked checkboxes or radio buttons.
The Pseudo Selector used is - :checked

Following are some simple examples to follow:
$("#chkBoxIdOrRadioButtId").is(":checked");
- returns a boolean true if checked or false
$("#chkBoxIdOrRadioButtId").not(":checked");
- returns a boolean true if unchecked or false
$("#chkBoxIdOrRadioButtId").attr('checked','checked');
- programmatically check the desired.
$("input:radio[name='myRadioGrpName']:checked").val();
- return the value of the checked radio from the 'myRadioGrpName' group.
$("input[name='myChkBoxGrp']:checked").length;
- returns the count of the checked
$("input[name='myChkBoxGrp']:not(:checked)").length;
- returns the count of the unchecked

jQuery : Get the number of options in a select box

Using jQuery to get the number of options in a select box
$('#mySelectBoxId option').length;

Using jQuery to get the number of options that are selected in a select box
$('#mySelectBoxId option:selected').length;

jQuery, CakePHP: Submit a form using ajax; implementing "quick save"

In CakePHP, If you want to submit a FORM using an AJAX request, use jQuery Form Plugin.

You can find a detailed example at http://marcgrabanski.com/article/cakephp-ajax-quick-save-jquery

An alternative to this plugin is invoking a javascript function, when a submit button is clicked, and which would read all the input data inside the form and send it to the controller.

Tcl/Tk: How to find out the Tcl version

How to find out the Tcl version

[yourLoginPrompt]$ tclsh
% info patchlevel
8.4.13
% puts $tcl_version
8.4
% info tclversion
8.4
%

Javascript: Make indexOf function working in IE

The indexOf function is a recent addition to the ECMA-262 standard and newly introduced in JavaScript 1.6, hence it may not work in some browsers. You can work around this by inserting the following piece of code at the beginning of your scripts.

Note: This function will work in FF1.5 or above, because this algorithm is exactly the one used in Firefox and SpiderMonkey(is the code-name for the Mozilla's C implementation of JavaScript).

if(!Array.indexOf) {
  Array.prototype.indexOf = function(obj) {
    for(var i = 0; i < this.length; i++) {
      if(this[i] == obj) { return i; }
    }

    return -1;
  }
}

Javascript: Easy Javascript Debugging using Firebug Console.

Instead of using the traditional 'alert' statements for debugging, the best way to debug is using Firebug console.
What you need : Firefox browser and Firebug addon

A simple example:
INSTEAD OF :

function foo() {
 if(some condition) {
    alert("Status update");
  }
}

USE :

function foo() {
 if(some condition) {
  console.log("Status update");
 }
}

For more details : http://getfirebug.com/logging

cakePHP: Passing multiple parameters from view to controller in cakePHP

There is no direct specification for passing multiple parameters from view (.ctp file) to controller (.php file) in cakePHP. It is intuitive !
The format of the url with a single parameter look like : ../myApp/myController/myFunction/parameter1

To pass multiple parameters simply append at the end which might look like
../myApp/myController/myFunction/parameter1/parameter2/parameter3

Some generic ways are:
1.
array ('action' = '/myFunction/'. $id .'/'.$another_id); 

2.
$Html-> link ('edit', array ('url'='/ myController/myFunction? Id ='. $Id. '& another_id ='. $another_id)). 


3.
$Html-> link ('edit', array ('action' = 'myFunction', $id,$another_id)). 


The function in myController would look like:
 function myFunction ($id, $another_id){
   // do something
}

Date formatting in JavaScript

We would be using 3 different methods to get the current system date, month and year.
  • getDate() : Returns the date
  • getMonth(): Returns the month
  • getFullYear(): Returns the year
#1: month/date/year (5/12/2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

document.write(sysMonth + "/" + sysDate + "/" + sysYear);

#2: date-month-year (12-May-2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

var monthNames = new Array("January", "February", "March",
               "April", "May", "June", "July", "August", 
               "September", "October", "November", 
               "December");

document.write(sysDate + "-" + monthNames[sysMonth] + "-" + sysYear);

#3: date-month-year (12th May 2010)

var date = new Date();
var sysDate = date.getDate();
var sysMonth = date.getMonth();
var sysYear = date.getFullYear();

var monthNames = new Array("January", "February", "March",
               "April", "May", "June", "July", "August", 
               "September", "October", "November", 
               "December");

var sup = "";

if(sysDate == 1 || sysDate == 21 || sysDate == 31) sup ="st";
else if (sysDate == 2 || sysDate == 22) sup = "nd";
else if (sysDate == 3 || sysDate == 23) sup = "rd";
else sup = "th";

document.write(sysDate + "" + sup + " " + monthNames[sysMonth] + " " + sysYear);

jQuery: Using jQuery with Other Libraries

Ever faced a problem to use jQuery with other Library ?
By default, jQuery uses "$" as a shortcut for "jQuery"

jQuery provides the convenience by defining our own shortcut which eliminates the conflict.
A simple example

<script src="jquery.js"> </script>
<script src="prototype.js"> </script>

<script type="text/javascript" >
jQuery.noConflict()  //now by default, $ is jQuery.
  //But you can reassign it to your own variable.
    var $jq = jQuery;
    //start using $jq for jquery's $
    $jq(document).ready(function() {
    $jq("div").hide();    });
</script>

Open a File Browser From Your Current Command Prompt/Terminal Directory


Open Finder in Mac OS X, the terminal type
open .

Open a File Browser in Windows, in the command prompt type:
explorer .
OR
start .

Open a File Browser in Linux, in the terminal type
nautilus .
OR
gnome-open .


PHP: Comparing dates/date strings in PHP

Comparing Date strings in PHP is easy using strtotime function.
The strtotime function returns # of seconds since Jan 1,1970 UTC.

Simple example:
$strtDate = strtotime("2009-10-3");
$endDate = strtotime("2010/11/9");
if($strtDate > $endDate){
 // do something
}
if($strtDate < $endDate){
 /do something
}

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...