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

Monday, 24 November 2014

Get url query string parameters using Javascript only

http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript

Make link open in parent of iframe

window.parent.location.href= "http://www.google.com";

Monday, 13 October 2014

Javascript associative array

Javascript does not feature associative arrays. However, you can create something similar using object arrays.

Example:

arr={'key1': 'value1','key2':'value2'};

This array can be accessed with:

the_value = array['key2']

Values can be added or changes:

arr['key3']='value3';

More info here: http://www.i-programmer.info/programming/javascript/1441-javascript-data-structures-the-associative-array.html

You can iterate over the object array using Jquery:

$.each( arr, function( index, value ){
sum += value;
});

Tuesday, 5 August 2014

To get a mysql formatted datetime using javascript:

**
 * You first need to create a formatting function to pad numbers to two digits…
 **/
function twoDigits(d) {
    if(0 <= d && d < 10) return "0" + d.toString();
    if(-10 < d && d < 0) return "-0" + (-1*d).toString();
    return d.toString();
}

/**
 * …and then create the method to output the date string as desired.
 * Some people hate using prototypes this way, but if you are going
 * to apply this to more than one Date object, having it as a prototype
 * makes sense.
 **/
Date.prototype.toMysqlFormat = function() {
    return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
};

I couldn't find a simpler jQuery solution to this.
If you want the time according to the user's time zone, remove the "UTC" in the second last line of the code.

Friday, 2 August 2013

In Internet Explorer 8, CSS adjacent sibling selectors don't work with javascript. Example, dynamically added classes in CSS selectors will be ignored by IE8.

Friday, 26 July 2013

In Javascript, to redirect the browser to another page:
window.location.href = "http://site.com/new_url";