From The Blog

Javascript: stop submitting form on pressing ENTER key

28 June 2014, in

//stop submitting form on pressing enter key

//This code will allow enter key on textarea
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = stopRKey;



Post a Comment

Javascript: have variable variables

26 November 2013, in

In PHP to have "Variable" variables.

For example

$x = "variable";

$$x = "hello, world!";

echo $variable;" //"hello, world!"

So as a workaround you can do as the following:

var obj = {}
obj.foo = 42;

var bar = 'foo';
console.log(obj[bar]);


Post a Comment

jQuery Colorbox: Hide background page vertical scroll bar on open

28 October 2013, in

To hide the scroll bar of the background page when the colorbox opens "like facebook for example"

 

/* make the page scroll dissapear when colorbox is open and retrieved back when colorbox is closed */
var screen_position;
$(document).bind('cbox_open', function() {
screen_position = $("body").scrollTop();
$('body').css({ overflow: 'hidden' });
$("body").scrollTop(0);
}).bind('cbox_closed', function() {
$('body').css({ overflow: 'auto' });
$("body").scrollTop(screen_position);
});



Post a Comment

HTML5 Javascript: get file input upload size locally

28 October 2013, in

if (typeof FileReader !== "undefined") {
var size = document.getElementById('upload_file').files[0].size;
size = Math.round(size / 1024);
console.log(size+' KB');
}



Post a Comment

How can I check if a jQuery plugin is loaded?

7 September 2013, in

Generally speaking, jQuery plugins are namespaces on the jQuery scope. You could run a simple check to see if the namespace exists:

if(jQuery().pluginName) {
     //run plugin dependent code
 }

,If we're talking about a proper jQuery plugin (on that extends the fn namespace), then the proper way to detect the plugin would be:

if($.fn.pluginname != 'undefined')

or
if($.fn.pluginname)


Post a Comment

How to know the mode the browser running with ?

7 April 2013, in

How to know the mode the browser running with ? i.e. Standards Mode, Quirks Mode ...

 

In Firefox and Opera you can determine if your browser is in "quirks mode" by checking page info.

Using document.compatMode, will tell you the mode you are in with most browsers.

In Chrome, Safari, and IE, run this javascript in the address bar:



Post a Comment

Javascript: Easily calculate difference between two dates in days

1 September 2012, in
Date.daysBetween = function( date1, date2 ) {
  //Get 1 day in milliseconds
  var one_day=1000*60*60*24;

  // Convert both dates to milliseconds
  var date1_ms = date1.getTime();
  var date2_ms = date2.getTime();

  // Calculate the difference in milliseconds
  var difference_ms = date2_ms - date1_ms;

  // Convert back to days and return
  return Math.round(difference_ms/one_day); 
}

//date1 and date2 are Date objects


Post a Comment

Javascript parsing issue on IE7

16 August 2012, in

I had a problem in a page that gets it's content via an ajax request and place the result of the ajax request in a slider, I was testing it on other browsers, and It came to IE, passed IE9 and IE8, and when it came to IE7 I couldn't see the slider with it's contents at all.

 

No error is shown in the console :-S, then I have figured it out......................

 

It is common in many programming languages that arrays and object attributes have a comma without the last element, and the parser/compiler/interpreter doesn't fire an error for that,



Post a Comment

How to detect client browser?

14 August 2012, in

/****** Browser Detection ******/
function testCSS(prop) {
return prop in document.documentElement.style;
}
var isOpera = !!(window.opera && window.opera.version); // Opera 8.0+
var isFirefox = testCSS('MozBoxSizing'); // FF 0.8+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !isSafari && testCSS('WebkitTransform'); // Chrome 1+



Post a Comment

Get base url using javascript

26 July 2012, in
function getBaseURL () {
   return location.protocol + "//" + location.hostname + 
      (location.port && ":" + location.port) + "/";
}


Post a Comment

Pages