JS中的一些小东西

javascript中存在几种对URL字符串进行编码的方法

javascript中存在几种对URL字符串进行编码的方法:escape(),encodeURI(),以及encodeURIComponent()。这几种编码所起的作用各不相同。
escape() 方法:

采用ISO Latin字符集对指定的字符串进行编码。所有的空格符、标点符号、特殊字符以及其他非ASCII字符都将被转化成%xx格式的字符编码(xx等于该字符在字符集表里面的编码的16进制数字)。比如,空格符对应的编码是%20。

不会被此方法编码的字符: @ * / +

encodeURI() 方法:

把URI字符串采用UTF-8编码格式转化成escape格式的字符串。

不会被此方法编码的字符:! @ # $& * ( ) = : / ; ? + '

encodeURIComponent() 方法:

把URI字符串采用UTF-8编码格式转化成escape格式的字符串。与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字符。所以如果字符串里面包含了URI的几个部分的话,不能用这个方法来进行编码,否则 / 字符被编码之后URL将显示错误。

不会被此方法编码的字符:! * ( ) '

因此,对于中文字符串来说,如果不希望把字符串编码格式转化成UTF-8格式的(比如原页面和目标页面的charset是一致的时候),只需要使用escape。如果你的页面是GB2312或者其他的编码,而接受参数的页面是UTF-8编码的,就要采用encodeURI或者encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之后引进的,escape则在javascript1.0版本就有。  

 


 

javascript中document.all和document.getElementBy**示例:

document.all:

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< title > Document.All Example </ title >
< meta  http-equiv ="content-type"  content ="text/html; charset=ISO-8859-1"   />
</ head >
< body >
< h1 > Example Heading </ h1 >
< hr  />
< p > This is a  < em > paragraph </ em > . It is only a  < em > paragraph. </ em ></ p >
< p > Yet another  < em > paragraph. </ em ></ p >
< p > This final  < em > paragraph </ em >  has  < em  id ="special" > special emphasis. </ em ></ p >
< hr  />
< script  type ="text/javascript" >
<!--
var i,origLength;
origLength 
= document.all.length;
document.write(
'document.all.length='+origLength+"<br />");
for (i = 0; i < origLength; i++)
{
document.write(
"document.all["+i+"]="+document.all[i].tagName+"<br />");
}

//-->
</ script >
</ body >
</ html >

document.getElementBy**

< html >
    
< head >
        
< title > 插入 </ title >
    
</ head >
    
< script  type ="text/javascript" >
         
function getValue()
          
{
              
var x=document.getElementById("myHeader")
              alert(x.innerHTML)
          }

          
function getElements()
      
{
             
var x=document.getElementsByName("sex");
              alert(x.length);
          }

  
          
function getTagElements()
          
{
              
var x = document.getElementsByTagName("input");
              alert(x.length);
          }

         
     
</ script >
    
< body >
        
< form  name ="Input" >
            
< table  align ="center"  width ="50%"  height ="50%"  border ="1" >
                
< tr >
                    
< td  align ="center"  width ="45%" >
                        学号
                    
</ td >
                    
< td  align ="center"  width ="55%" >
                        
< input  type ="text"  id =userid  name ="user"  onblur ="validate();" >
                        
< div  id =usermsg ></ div >
                    
</ td >
                
</ tr >
                
< tr >
                    
< td  align ="center"  width ="45%" >
                        姓名
                    
</ td >
                        
< td  align ="center" >
                        
< input  type ="text"  name ="name" >
                    
</ td >
                
</ tr >
                
< tr >
                    
< td  align ="center"  width ="%45" >
                        性别
                    
</ td >
                    
< td  align ="center" >
                        
< input  type ="radio"  name ="sex"  value ="男" >
                        男
                        
< input  type ="radio"  name ="sex"  value ="女" >
                        女
                    
</ td >
                
</ tr >
                
< tr >
                    
< td  align ="center"  width ="45%" >
                        年龄
                    
</ td >
                    
< td  align ="center"  width ="55%" >
                        
< input  type ="text"  name ="age" >
                    
</ td >
                
</ tr >
                
< tr >
                    
< td  align ="center"  width ="45%" >
                        籍贯
                    
</ td >
                    
< td  align ="center"  width ="55%" >
                        
< input  type ="text"  name ="addr" >
                    
</ td >
                
</ tr >

            
</ table >
        
</ form >
        
< h1  id ="myHeader"  onclick ="getValue()" >
            This is a header
        
</ h1 >
        
< p >
            Click on the header to alert its value
        
</ p >
        
< input  type ="button"  onclick ="getElements()"
            value
="How many elements named 'sex'?"   />
        
< Br >
        
< input  type ="button"  onclick ="getTagElements()"
            value
="How many tags named 'input'?"   />

    
</ body >
</ html >

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值