October 2011

setInterval() and clearInterval() Example

var t;
 
function show_uploaded_logo() {
   if($('div.form-item div.image-widget div.image-preview img').is('*'))
   {
      // use console.log to test that the function has stopped running
      //console.log('running');
      if(!$('img#step-1-preview-image').is('*'))
      {
            //code here
      }
      else
      {
           clearInterval(t);	
      }
 
   }   
}
 
t = setInterval(show_uploaded_logo,1000);

Use jQuery to hide a DIV when the user clicks outside of it

$('div#personalize-window').hover(function(){
     mouse_is_inside = true;
},
function()
{ 
     mouse_is_inside=false; 
});
 
 
$("body").mouseup(function(){
     if(! mouse_is_inside)  $('div#personalize-window').fadeOut('slow');
});

PHP Get Current URL

Sometimes it is not as straightforward as one may think to get the current url to use it inside your application. Here is a snippet that I use to fetch the current URL and use it in a script. The current url (whether http or https) is now a local variable that you can do with as you please.

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] :
 
"http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

You should groom this value before using in anything sensitive, like a sql query.

Mobile Version of Website

To implement our changes, we first set the viewport to the device's width. Do this by inserting the following line of code into the head of your XHTML template:

chown and chgrp in one command

chown user:group filename.ext

Of Course, to change all of the files in a folder:

chown user:group folder/*

And then, once you know that, you can do multiple files/folders at once:

chown user:group folder folder/* filename.ext filename2.ext

Mobile Device Detection in PHP

<?php
 
function mobile_device_detect($iphone=true,$ipad=true,$android=true,$opera=true,$blackberry=true,
$palm=true,$windows=true,$mobileredirect=false,$desktopredirect=false){
 
  $mobile_browser   = false; 
// set mobile browser as false till we can prove otherwise
 
  $user_agent       = $_SERVER['HTTP_USER_AGENT']; 
// get the user agent value - this should be cleaned to ensure no nefarious input gets executed
 
  $accept           = $_SERVER['HTTP_ACCEPT']; 
// get the content accept value - this should be cleaned to ensure no nefarious input gets executed
 
  switch(tr