Created
May 8, 2015 12:04
-
-
Save bjrnqprs/8813a8fca1f919f53197 to your computer and use it in GitHub Desktop.
Collection of Javascript functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCookieValue(key) { | |
currentcookie = document.cookie; | |
if (currentcookie.length > 0) | |
{ | |
firstidx = currentcookie.indexOf(key + "="); | |
if (firstidx != -1) | |
{ | |
firstidx = firstidx + key.length + 1; | |
lastidx = currentcookie.indexOf(";",firstidx); | |
if (lastidx == -1) | |
{ | |
lastidx = currentcookie.length; | |
} | |
return unescape(currentcookie.substring(firstidx, lastidx)); | |
} | |
} | |
return ""; | |
} | |
/** | |
* Set a cookie. | |
* | |
* @param cookieName | |
* @param value | |
* @param expireDays Optional. Defaults to 1 day. | |
* @param path Optional. Defaults to / | |
* @returns {boolean} | |
*/ | |
function setCookie(cookieName, value, expireDays, path) { | |
if(typeof cookieName === 'undefined' || typeof value == 'undefined') { | |
return false; | |
} | |
if(typeof expireDays === 'undefined') { | |
expireDays = 1; | |
} | |
if(typeof path === 'undefined') { | |
path = '/'; | |
} | |
var cookieExpireDays = expireDays * 24 * 60 * 60 * 1000; | |
var currentDate = new Date(); | |
var expirationDate = new Date(currentDate.getTime() + cookieExpireDays); | |
document.cookie = cookieName + '=' + value + ';' | |
+ 'expires=' + expirationDate.toGMTString() + ';' | |
+ 'path=' + path; | |
return true; | |
} | |
/** | |
* Opens a popup in the center of the screen | |
* | |
* @param url | |
* @param title | |
* @param width | |
* @param height | |
* @returns {window} | |
*/ | |
function openPopupCenter(url, title, width, height) { | |
var top = (screen.height/2)-(height/2); | |
var left = (screen.width/2)-(width/2); | |
return window.open( | |
url, title, | |
'width='+width+',height='+height+',top='+top+',left='+left+',resizable=yes' | |
); | |
} | |
function nl2br (str, is_xhtml) { | |
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>'; | |
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2'); | |
} | |
/** | |
* Get back a GET parameter, properly decoded. Or null if not found. | |
* | |
* @param string name | |
* @returns string|null | |
*/ | |
function getURLParameter(name) { | |
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null; | |
} | |
/** | |
* Return a random value from the given non-associative array | |
* | |
* @param array | |
* @returns {*} | |
*/ | |
function getRandomArrayValue(array) { | |
return array[Math.floor(Math.random() * array.length)]; | |
} | |
/** | |
* Send data to the given URL via a POST request. Or GET if specified. | |
* | |
* @param string path | |
* @param object params | |
* @param string method | |
*/ | |
function postToURL(path, params, method) { | |
method = method || "post"; // Set method to post by default if not specified. | |
// The rest of this code assumes you are not using a library. | |
// It can be made less wordy if you use one. | |
var form = document.createElement("form"); | |
form.setAttribute("method", method); | |
form.setAttribute("action", path); | |
for(var key in params) { | |
if(params.hasOwnProperty(key)) { | |
var hiddenField = document.createElement("input"); | |
hiddenField.setAttribute("type", "hidden"); | |
hiddenField.setAttribute("name", key); | |
hiddenField.setAttribute("value", params[key]); | |
form.appendChild(hiddenField); | |
} | |
} | |
document.body.appendChild(form); | |
form.submit(); | |
} | |
// ---------------------------------------------------------- | |
// If you're not in IE (or IE version is less than 5) then: | |
// ie === undefined | |
// If you're in IE (>=5) then you can determine which version: | |
// ie === 7; // IE7 | |
// Thus, to detect IE: | |
// if (ie) {} | |
// And to detect the version: | |
// ie === 6 // IE6 | |
// ie > 7 // IE8, IE9, IE10 ... | |
// ie < 9 // Anything less than IE9 | |
// ---------------------------------------------------------- | |
var ie = (function(){ | |
var undef,rv = -1; // Return value assumes failure. | |
if (navigator.appName == 'Microsoft Internet Explorer') | |
{ | |
var ua = navigator.userAgent; | |
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); | |
if (re.exec(ua) != null) | |
rv = parseFloat( RegExp.$1 ); | |
} | |
return ((rv > -1) ? rv : undef); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment