在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>
答案 0 :(得分:0)
尝试将querystring作为第二个参数传递:
$(".myDIV").load("page.html", { city: 'London' });
答案 1 :(得分:0)
我认为您的问题是您尝试使用javascript而不是服务器端检索查询字符串。
虽然服务器会看到您在load
内传递的查询字符串,但javascript会看到当前的网址。
例如,您可以执行以下操作:
<script>
var city = "<?php echo $_GET['city']; ?>";
</script>