HTML文件如何从URL中获取传递的参数值示例源代码
1个回答
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
//sArgName表示要获取哪个参数的值
function GetArgsFromHref(sArgName)
{
var sHref = "Untitled-2.html?id=2"
//CuPlayer.com提示:测试数据
//实际情况是用window.location.href得到URL
alert(sHref);
var args = sHref.split("?");
var retval = "";
if(args[0] == sHref) /*参数为空*/
{
return retval; /*CuPlayer.com提示:无需做任何处理*/
}
var str = args[1];
args = str.split("&");
for(var i = 0; i < args.length; i++ )
{
str = args[i];
var arg = str.split("=");
if(arg.length <= 1) continue;
if(arg[0] == sArgName) retval = arg[1];
}
//return retval;
alert(retval);
}
</script>
</head>
<input type="button" value="cuplayer.com参数" onclick="GetArgsFromHref('id');" />
<body>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询