随笔-3  评论-26  文章-41  trackbacks-0
/*
获取URL中最后一项参数的值
*/


var str=window.location.href;
//alert(str);
var es=/SouceID=/
es.exec(str); 
var right=RegExp.rightContext; 
//alert(right);
   
  //列子
    <script language="javascript"> 
    var str=window.location.href; 
    var es=/SouceID=/; 
    es.exec(str); 
    var right=RegExp.rightContext; 
    //alert(right);
    switch(right){
    case 'Din':
    case 'Exh':
    case 'Banks':
    case 'Shop':
    case 'Treat':
    case 'Trip':
         ChgTab('tab3','tabcontent3');
         break;
    case 'Air':
    case 'Railway':
    case 'Road':
    case 'Subway':
          ChgTab('tab2','tabcontent2');
          break;
   default:
         ChgTab('tab1','tabcontent1');
}
</script>


//以下是函数的写法
function GetParam(){
    
var url = document.location.href;
    
var name=""
    
if (url.indexOf("=")>0)
    
{
        name 
= url.substring(url.indexOf("=")+1,url.length)
    }

    
return name;
}



/*
获取指定的URL参数值
URL:http://www.blogjava.net/blog?name=bainian
参数:paramName URL参数
调用方法:getParam("name")
返回值:bainian
*/

//1.
function getParam(paramName)
{
        paramValue 
= "";
        isFound 
= false;
        
if (this.location.search.indexOf("?"== 0 && this.location.search.indexOf("=")>1)
        
{
            arrSource 
= unescape(this.location.search).substring(1,this.location.search.length).split("&");
            i 
= 0;
            
while (i < arrSource.length && !isFound)
            
{
                
if (arrSource[i].indexOf("="> 0)
                
{
                     
if (arrSource[i].split("=")[0].toLowerCase()==paramName.toLowerCase())
                     
{
                        paramValue 
= arrSource[i].split("=")[1];
                        isFound 
= true;
                     }

                }

                i
++;
            }
   
        }

   
return paramValue;
}



//2.
function Request(sName)
{

  
/*
   get last loc. of ?
   right: find first loc. of sName
   +2
   retrieve value before next &
  
  
*/

  
  
var sURL = new String(window.location);
  
var sURL = document.location.href;
  
var iQMark= sURL.lastIndexOf('?');
  
var iLensName=sName.length;
  
  
//retrieve loc. of sName
  var iStart = sURL.indexOf('?+ sName +'=') //limitation 1
  if (iStart==-1)
        
{//not found at start
        iStart = sURL.indexOf('&+ sName +'=')//limitation 1
                if (iStart==-1)
                   
{//not found at end
                    return 0//not found
                   }
   
        }

        
  iStart 
= iStart + + iLensName + 2;
  
var iTemp= sURL.indexOf('&',iStart); //next pair start
  if (iTemp ==-1)
                
{//EOF
                iTemp=sURL.length;
                }
  
  
return sURL.slice(iStart,iTemp ) ;
  sURL
=null;//destroy String
}
posted on 2008-03-12 12:53 百年 阅读(37272) 评论(5)  编辑  收藏 所属分类: Javascript Article

评论:
# re: js获取URL中的参数值[未登录] 2012-10-09 23:04 | lee
获取url中的参数值  回复  更多评论
  
# re: js获取URL中的参数值[未登录] 2013-02-09 08:43 | 匿名
感谢分享.  回复  更多评论
  
# re: js获取URL中的参数值 2013-08-06 10:58 | afasfas
feafwaefeawfewaef  回复  更多评论
  
# re: js获取URL中的参数值 2013-08-06 10:58 | afasfas
@afasfas
@afasfas
  回复  更多评论
  
# re: js获取URL中的参数值 2014-08-26 14:11 | 赵元春
网址打不开  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: