window.location传递参数

来源:互联网 发布:阿里云盒子系统升级 编辑:程序博客网 时间:2024/06/06 01:15

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="text" name="text1" id="text1" value="" />
<input type="button" name="btn1" id="btn1" value="点击" />
</body>
<script src="jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">


$('#btn1').on('click',function(){
var textValue = $('#text1').val();

window.location.href = '2.html#&value_='+textValue;
})

</script>
</html>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<input type="button" name="btn2" id="btn2" value="dongtao" />
<div class="div1"></div>
</body>
<script src="jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$(function(){

var value = getHash('value_');
$('#btn2').on('click',function(){
$('.div1').html(value);
})
})

//获取路径参数
function getHash(key, url) {
var hash;
if (!!url) {
hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1");
hash = (hash == url) ? "" : hash;
} else {
hash = self.location.hash;
}

hash = "" + hash;
hash = hash.replace(/^[?#]/, '');
hash = "&" + hash;
var val = hash.match(new RegExp("[\&]" + key + "=([^\&]+)", "i"));
if (val == null || val.length < 1) {
return null;
} else {
return decodeURIComponent(val[1]);
}
}
</script>
</html>

0 0
原创粉丝点击