您的位置:首页 > Web前端 > JavaScript

js获取url传递参数

2014-11-12 19:39 309 查看
</pre><pre name="code" class="html">// JavaScript Document
var num;//用于获取点击的是z左侧第几个
// 页面跳转  有地址传入 执行
if (location.search != null)
{
var Request = new Object();
Request = GetRequest();
//获得参数数组
var par = Request["id"];
setTab(par);
}
//js获取url传递参数
function GetRequest()
{
var url = location.search;
//获取url中"?"符后的字串 ?id=参数
var theRequest = new Object();
if (url.indexOf("?") != - 1)
{
//判读是否存在 ? 就是判断是否有参数
var str = url.substr(1);
//获得 id=参数
if (str.indexOf("&") != - 1)
{
//有多个参数
strs = str.split("&");
for (var i = 0; i < strs.length; i++)
{
theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
//decodeURI()解码
}
}
else
{
//只有一个参数
theRequest[str.split("=")[0]] = decodeURI(str.split("=")[1]);
//theRequest[id]=参数
}
}
//alert(theRequest["id"]);
return theRequest;
}
// --------------------------------------------------------------
//左侧导航样式改变
function setTab(n)
{
num = n;
var tdd = document.getElementById("menu").getElementsByTagName("dd");
/*获取id是menu 的dl 的dd对象*/
for (i = 0; i < tdd.length; i++) {
tdd[i].className = i == n ? "active" : "";
/*更改选项卡的dd对象的样式,如果是选定的项则使用.active样式*/
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  javascript