js html url编码,js URLdecode()与urlencode方法支持中文解码

下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。

代码如下

复制代码

Function str2asc(strstr)

str2asc = hex(asc(strstr))

End Function

Function asc2str(ascasc)

asc2str = chr(ascasc)

End Function

function UrlEncode(str){

var ret="";

var strSpecial="!"#$%&'()*+,/:;<=>?[]^`{|}~%";

for(var i=0;i

var chr = str.charAt(i);

var c=str2asc(chr);

if(parseInt("0x"+c) > 0x7f){

ret+="%"+c.slice(0,2)+"%"+c.slice(-2);

}else{

if(chr==" ")

ret+="+";

else if(strSpecial.indexOf(chr)!=-1)

ret+="%"+c.toString(16);

else

ret+=chr;

}

}

return ret;

}

用这样的办法调用:

代码如下

复制代码

alert(UrlEncode("孙毓波大好人"));

解码办法

代码如下

复制代码

function UrlDecode(zipStr){

var uzipStr="";

for(var i=0;i

var chr = zipStr.charAt(i);

if(chr == "+"){

uzipStr+=" ";

}else if(chr=="%"){

var asc = zipStr.substring(i+1,i+3);

if(parseInt("0x"+asc)>0x7f){

uzipStr+=decodeURI("%"+asc.toString()+zipStr.substring(i+3,i+9).toString());

i+=8;

}else{

uzipStr+=AsciiToString(parseInt("0x"+asc));

i+=2;

}

}else{

uzipStr+= chr;

}

}

return uzipStr;

}

function StringToAscii(str){

return str.charCodeAt(0).toString(16);

}

function AsciiToString(asccode){

return String.fromCharCode(asccode);

}

用法

使用方法

代码如下

复制代码

var str = '%e7%90%bc%e5%8f%b0%e5%8d%9a%e5%ae%a2';

var destr = UrlDecode(str);

alert(destr);

javascript中弹出< /script>标记出错解决方法,有碰到这样问题的机友可参考参考。

在JS里弹出‘’会让JS误以为这是一个结束标签,而出现异常。

如以下代码

eca03deadb5060bc0c5c8df91edfb843.png

我们在 ’字符串,结果被误以为是结束标签,VIM还变了颜色。运行结果

1b6d21ec36099c99d12652a1eee6ebe9.png

由于把字符串里的当成了结束标签,所以就出现异常,显然这不是我们想要的结果。我们只需要把代码稍加处理一下即可实现。

40563f5e8a02337568e00f889df0e449.png

在‘’里使用加号链接字符串,再来看下运行结果

55fd41cc5dcb16f8817c5e2f1901d74b.png

程序正常,正确弹出字符串'',在JS中如果字符串使用加号等于连接,数字使用加号等于相加

在js中没有像php中strtotime()与date()函数,可直接转换时间戳,下面我们来自定一个函数来实现js中具体有时间戳转换的功能。 代码如下

复制代码

function datetime_to_unix(datetime){

var tmp_datetime = datetime.replace(/:/g,'-');

tmp_datetime = tmp_datetime.replace(/ /g,'-');

var arr = tmp_datetime.split("-");

var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));

return parseInt(now.getTime()/1000);

}

function unix_to_datetime(unix) {

var now = new Date(parseInt(unix) * 1000);

return now.toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");

}

var datetime = '2012-11-16 10:36:50';

var unix = datetime_to_unix(datetime);

document.write(datetime+' 转换后的时间戳为: '+unix+'

');

var unix = 1353033300;

var datetime = unix_to_datetime(unix);

document.write(unix+' 转换后的日期为: '+datetime);

如果想弹出:2010-10-20 10:00:00这个格式的也好办

代码如下

复制代码

function getLocalTime(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");

}

alert(getLocalTime(1177824835));

完整实例

代码如下

复制代码

var day1 = parseInt(new Date().valueOf()/1000);

var day2 = new Date(day1 * 1000);

function getLocalTime(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:d{1,2}$/,' ');

}

/* 同上面函数 */

function getLocalTimes(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17);

}

function getLocalFormatTime(nS) {

return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");

}

document.getElementById("btn1").onclick = function(){

alert(day1);

}

document.getElementById("btn2").onclick = function(){

alert(day2.toLocaleString());

}

document.getElementById("btn3").onclick = function(){

alert( getLocalTime(day1) );

}

document.getElementById("btn4").onclick = function(){

alert( getLocalFormatTime(day1) );

}

document.getElementById("btn5").onclick = function(){

alert(day2.getFullYear()+"-"+(day2.getMonth()+1)+"-"+day2.getDate()+" "+day2.getHours()+":"+day2.getMinutes()+":"+day2.getSeconds());

}

获取农历

农历月的天数是一个变数,有时是 29 天,有时是 30 天。

农历每月的第一天是月亮全黑的日子。

农历年由 24 个节气来确定,节气则由太阳的角度来确定。农历的第一个节气叫 雨水,定在太阳的角度为 330 度的日子。其余的 23 个节气分别定在太阳的角度 每变化 15 度的日子。下面的表格列出了 24 个节气的名称和定义:

命称    角度    公历日期      周期

立春     315     2月 4日

雨水     330     2月19日    29.8天

惊蛰     345     3月 6日

春分       0     3月21日    30.2天

清明      15     4月 5日

谷雨      30     4月20日    30.7天

立夏      45     5月 6日

夏满      60     5月21日    31.2天

芒种      75     6月 6日

夏至      90     6月22日    31.4天

小暑     105     7月 7日

大暑     120     7月23日    31.4天

立秋     135     8月 8日

处暑     150     8月23日    31.1天

白露     165     9月 8日

秋分     180     9月23日    30.7天

寒露     195    10月 8日

霜降     210    10月24日    30.1天

立冬     225    11月 8日

小雪     240    11月22日    29.7天

大雪     255    12月 7日

冬至     270    12月22日    29.5天

小寒     285     1月 6日

大寒     300     1月20日    29.5天

24 个节气中有 12 个是主节气:雨水,春分,谷雨,夏满,夏至,大暑,处暑, 秋分,霜降,小雪,冬至,大寒。

农历年跟天文年相差较大。农历常年有十二个农历月,有 353,354,或者 355 天,比天文年少大约 11 天。为了跟天文年同步,每隔三个农历常年左右,必需设 一闰年。闰年有十三个月,添加的这个月叫闰月。

农历十二个月的名称分别为:正月,二月,三月,四月,五月,六月,七月,八 月,九月,十月,冬月,腊月。

农历闰年闰月的确定比较难,闰月农历算法有两条:

•一:冬至必须落在农历冬月。如果落不上,腊月之前就要添上一个月,成为闰 年。

•二:如果是闰年,冬月后边第一个不含主节气的月份定为闰月。闰月使用前一 月份的名称。

农历年以 60 年为一周期,每年的名称由 10 个天干的一个字和 12 个地支的一 个字排列而成。

10 天干为:

•甲,乙,丙,丁,戊,己,庚,辛,壬,癸。

12 地支 为:

•子,丑,寅,卯,辰,巳,午,未,申,酉,戌,亥。

•12 地支有 12 动物生肖 与其对应:

鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪。

跟据历史记载,农历年已经经过了 78 个周期。今年,公历2010 年,是第 79 个周期的第 28 年,也就是农历第 4707 年

代码如下

复制代码

如果要在js中来判断自身是否是顶级窗口我们可直接利用top.location!=self.location来加以操作,具体看下面代码

顶级窗口的地址:top.location

本窗口的地址:self.location

代码如下

复制代码

if(top.location!=self.location){

top.location = self.location;

}

具体实现程序

代码如下

复制代码

if (window.top !== window.self) { // are you trying to put self in an iframe?

try {

if (window.top.location.host) { // this is illegal to access unless you share a non-spoofable document domain

// fun times

} else {

bust(); // chrome executes this

}

} catch (ex) {

bust(); // everyone executes this

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值