Scripts
[JS]_GET/_COOKIE
Keihard en schaamteloos gejat van : http://dieterraber.net/index.php?topic=js&subtopic=RequestVars Gebruik het best vaak in combinatie met AJAX om het moment dat ik een getvar in een ajax request wil proppen :) Als iemand er niet blij mee is moet hij/zij het maar bij bas aangeven maar denk dat het wel een practische toevoeging is
jsgetcookie
[code]
/**
* requestVars.js
*
* This file contains functions to access the content of cookies
* and GET parameters easily for javascript.
* They are inspired by their PHP equivalent.
*
* This source file is subject to version 2.1 of the GNU Lesser
* General Public License (LPGL), found in the file LICENSE that is
* included with this package, and is also available at
* http://www.gnu.org/copyleft/lesser.html.
* @package Javascript
*
* @author Dieter Raber <[email protected]>
* @copyright 2004-12-27
* @version 1.0
* @license http://www.gnu.org/copyleft/lesser.html
*
*/
/**
* TOC
*
* - readGet
* - readCookies
*/
/**
* _GET is the equivalent to PHP's $_GET
* An associative array of variables passed to the current script
* via the HTTP GET method. This function is called onload.
* This means that as soon as this file is included _GET will be available
*
* example:
* If the curent URL contains GET parameters e.g.
* myurl.htm?color1=red&color2=blue
* then _GET['color1'] will contain 'red'
* and _GET['color2'] will contain 'blue'
*/
function readGet()
{
var _GET = new Array();
var uriStr = window.location.href.replace(/&/g, '&');
var paraArr, paraSplit;
if (uriStr.indexOf('?') > -1)
{
var uriArr = uriStr.split('?');
var paraStr = uriArr[1];
}
else
{
return _GET;
}
if (paraStr.indexOf('&') > -1)
{
paraArr = paraStr.split('&');
}
else
{
paraArr = new Array(paraStr)
}
for(var i = 0; i < paraArr.length; i++)
{
paraArr[i] = paraArr[i].indexOf('=') > -1
? paraArr[i]
: paraArr[i] + '=';
paraSplit = paraArr[i].split('=')
_GET[paraSplit[0]] = decodeURI(paraSplit[1].replace(/\+/g, ' '));
}
return _GET;
}
var _GET = readGet();
/**
* _COOKIE is the equivalent to PHP's $_COOKIE
* An associative array of variables passed to the current script
* via the HTTP COOKIE method. This function is called onload.
* This means that as soon as this file is included _COOKIE will be available
*
* example:
* This function is called onload
* If two cookies are set, e.g.
* foo=yellow and bar=pink
* then _COOKIE['foo'] will contain 'yellow'
* and _COOKIE['bar'] will contain 'pink'
*/
function readCookie()
{
var _COOKIE = new Array()
var cookieStr = document.cookie ? document.cookie : '';
var cookieSplit, cookieKey;
if(cookieStr)
{
cookieStr = cookieStr.replace(/; /, ';')
var cookieArr = cookieStr.indexOf(';') > -1
? cookieStr.split(';')
: new Array(document.cookie);
for(var i = 0; i < cookieArr.length; i++)
{
cookieArr[i].indexOf('=') > -1 ? cookieArr[i] : cookieArr[i] + '=';
cookieSplit = cookieArr[i].split('=');
cookieKey = cookieSplit[0].charAt(0) == ' '
? cookieSplit[0].substring(1, cookieSplit[0].length)
: cookieSplit[0];
_COOKIE[cookieKey] = cookieSplit[1].replace(/\+/g, ' ');
}
}
return _COOKIE
}
var _COOKIE = readCookie();
[/code]
Reacties
0