Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Thursday, August 9, 2018

Handling undefined & null parameters in JavaScript

Here with a given example is enough to make it understand


 function foo(DivID, color) {  
   if (!color) { // the correct test here should be 'typeof color == "undefined"'  
     color = "green";  
   }  
   DivID.style.backgroundColor = color;  
 }  
 foo(div_color, "red"); //set the background color to red  
 foo(div_color); //set the background color to the default green  
 foo(div_color, "");​ //remove the background color !This will fail!  

Thursday, May 12, 2016

Popup new window center screen php javascript

First Create a javascript function for popup window. w=width , h=height of the window
 // Popup windows   
 function popupwindow(url, title, w, h) {  
        var left = (screen.width/2)-(w/2);  
        var top = (screen.height/2)-(h/2);  
        return window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);  
 }  

Now make a php function to use this java-script function everywhere in php easily.

 /**  
  * Return pop up   
  * @author Sharif Ahmed  
  * @param str url  
  * @param str title/value  
  * @param int width  
  * @param int height  
  * @return return popup center windows  
  */  
 function my_anchor_popup($url,$title,$width,$height)   
   {  
     return '<a href="'.$url.'" onclick="popupwindow(\''.$url."','".$title."', '".$width."','".$height."'); return false;\">".$title.'</a>';  
   }   

Wednesday, February 24, 2016

Count and display number of characters in a textbox using Javascript & Jquery

We could do this in jQuery, assuming we want the character count displayed in a div with id="characters"


<textarea></textarea>
<span id="characters"><span>
$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);

function updateCount() {
    var cs = $(this).val().length;
    $('#characters').text(cs);
}
This is the most easiest way to do it.

Total Pageviews