今天要写个用Javascript读取Asp.net中生成的Cookie值的功能, 因我的.net项目中用到了多个Cookie,上CSDN、Baidu、Google搜了一大堆Javascript读取Cookie的公用函数,试了下,全不行,它们基本都是针对单个Cookie的,一遇到多个Cookie的情况,全混乱了。没办法,仔细研究了下客户端存放Cookie的格式,发现多个Cookie无非是以分号分隔的串,于是自己写了个函数来完成读取多个Cookie值:
//
objName为Cookie对象名称,keyName为Cookie键名称
function getCookie(objName,keyName)
{
var result = "" ;
// 如果Cookie长度大于0
if (document.cookie.length > 0 )
{
// 包含多个cookie的数组
var objArray = document.cookie.split( " ; " );
// Cookie对象名
var objSearch = objName + " = " ;
// Cookie键名称
var nameSearch = keyName + " = " ;
for ( var objItem in objArray)
{
// 如果数组中包含对象名
if (objArray[objItem].indexOf(objSearch) != - 1 )
{
// 在数组中取Cookie值
begin = objArray[objItem].indexOf(nameSearch);
if (begin != - 1 )
{
begin += nameSearch.length;
end = objArray[objItem].indexOf( " & " ,begin);
if (end == - 1 )
{
end = objArray[objItem].length;
}
result = decodeURI(objArray[objItem].substring(begin, end));
}
}
}
}
return result;
}
function getCookie(objName,keyName)
{
var result = "" ;
// 如果Cookie长度大于0
if (document.cookie.length > 0 )
{
// 包含多个cookie的数组
var objArray = document.cookie.split( " ; " );
// Cookie对象名
var objSearch = objName + " = " ;
// Cookie键名称
var nameSearch = keyName + " = " ;
for ( var objItem in objArray)
{
// 如果数组中包含对象名
if (objArray[objItem].indexOf(objSearch) != - 1 )
{
// 在数组中取Cookie值
begin = objArray[objItem].indexOf(nameSearch);
if (begin != - 1 )
{
begin += nameSearch.length;
end = objArray[objItem].indexOf( " & " ,begin);
if (end == - 1 )
{
end = objArray[objItem].length;
}
result = decodeURI(objArray[objItem].substring(begin, end));
}
}
}
}
return result;
}
一个类似于.net中Request.QueryString的获取地址栏GET参数的函数:
//
获取GET参数值
function getQueryString(pName)
{
var result = "" ;
var url = unescape(window.location.href);
var parameters = url.substr(url.indexOf( " ? " ) + 1 );
var parametersArray = parameters.split( " & " );
for ( var i in parametersArray)
{
parameterName = parametersArray[i].split( " = " )[ 0 ];
parameterValue = parametersArray[i].split( " = " )[ 1 ];
if ( pName == parameterName)
{
result = parameterValue;
}
}
return result;
}
function getQueryString(pName)
{
var result = "" ;
var url = unescape(window.location.href);
var parameters = url.substr(url.indexOf( " ? " ) + 1 );
var parametersArray = parameters.split( " & " );
for ( var i in parametersArray)
{
parameterName = parametersArray[i].split( " = " )[ 0 ];
parameterValue = parametersArray[i].split( " = " )[ 1 ];
if ( pName == parameterName)
{
result = parameterValue;
}
}
return result;
}
获取当前时间的数字形式,常用作随机数
//
用当前时间作随机数
function getRandom()
{
var result = new Date();
return result.getTime();
}
function getRandom()
{
var result = new Date();
return result.getTime();
}
常见的鼠标指向时全选功能
<
input type
=
"
text
"
name
=
"
test
"
onMouseOver
=
"
this.select();
"
value
=
"
测试
"
/>
复制到我的剪帖板功能
function
copyH()
{
obj = Form1.txtHtml;
obj.select();
js = obj.createTextRange();
js.execCommand( " Copy " );
}
{
obj = Form1.txtHtml;
obj.select();
js = obj.createTextRange();
js.execCommand( " Copy " );
}
截取文件扩展名
function getEx(str)
{
var begin = str.lastIndexOf( " . " );
var end = str.length;
return str.substring(begin,end).toUpperCase();
}
{
var begin = str.lastIndexOf( " . " );
var end = str.length;
return str.substring(begin,end).toUpperCase();
}
在客户端预览图片,并显示图片长宽
function
getimg1(self)
{
var image = new Image();
image.src = self.value;
imgshow1.innerHTML = " <img src=' " + self.value + " ' width='120' height='90'> " ;
txtshow1.innerHTML = " <font color='ff0000'> " + image.width + " x " + image.height + " </font> " ;
}
{
var image = new Image();
image.src = self.value;
imgshow1.innerHTML = " <img src=' " + self.value + " ' width='120' height='90'> " ;
txtshow1.innerHTML = " <font color='ff0000'> " + image.width + " x " + image.height + " </font> " ;
}
Javascript中的编码与解码
.
.
.
.