【发布时间】:2016-04-10 23:03:02
【问题描述】:
在 Index.html 我有:
<head>
<script>
$(function(){ $(".myDIV").load("page.htm?city=London"); });
</script>
</head>
<body>
<div class="myDIV"></div>
</body>
除了参数 ?city=London 外,一切正常
我的意思是 page.htm 在 index.htm 中打开,但参数 city 不可见。 它应该有效吗?
在 page.htm 我有
<script>
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
</script>
最后
<div class="city"></div>
<script>
var cityVar= getUrlParameter('city');
$('.city').html(cityVar);
</script>
【问题讨论】:
-
load函数获取您当前网址的查询字符串。尝试访问index.html?city=somethingElse看看会发生什么。
标签: javascript