基于AJAX的网站中的JS window.location
JS window.location in an AJAX based site
我有一个简单的基于AJAX的网站,我有同样简单的地理定位功能,当用户点击按钮时,它会将用户重定向到新页面。
这是我的地理定位功能的一部分,它重定向用户,正如你所看到的,它将人们重定向到一个名为weather.php的页面;
function showPosition(position) {
window.location='weather.php?lat='+position.coords.latitude+'&long='+position.coords.longitude;
}
问题是,它使用"传统"页面加载重定向页面,从而使我实现的AJAX页面加载过时。
有可能修改这个函数并使其对AJAX友好吗?
这是完整的重定向代码;
<button onclick="getLocation()">My Location</button>
<script>
var x = document.getElementById("message");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
window.location='weather.php?lat='+position.coords.latitude+'&long='+position.coords.longitude;
}
<script>
以下是如何做到这一点。我给您举一个简单的网站示例,其中页面是用Ajax加载的。页面被放在url中。
不是这样的:weather.php?lat=50.452
但是像这样:index.php#50.452
关键是:当您更改#右侧的任何内容时,页面不会重新加载。尽管如此,还是有一个系统,url会记住页面
这不是你问题的完整答案,但我希望它能给你所需的灵感
pages.php
<?php
switch(isset($_GET['p']) ? $_GET['p'] : '') {
default:
$content = 'This is the HOME page ...';
break;
case 'calendar':
$content = 'calendar stuff ...';
break;
case 'contact':
$content = 'contact stuff ...';
break;
}
echo $content;
?>
index.php
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
// load page according to the url.
// We read the url, to the right hand side of the # (hash symbol)
var hash = window.location.hash;
var page = hash.substr(1); // this removes the first character (the #)
loadPage(page);
// triggered by the navigation.
function loadPage(page) {
getAjaxPage(page);
}
// this returns the content of a page
function getAjaxPage(page) {
switch (page) {
default: // = HOME page
var url = '';
case 'calendar':
case 'contact':
var url = page;
}
$.ajax({
url: 'pages.php',
data: {
p: url
},
success: function(data) {
$('#content').html(data);
}
})
}
</script>
<style>
#content {
margin: 15px;
}
</style>
</head>
<body>
<div id="nav">
<a href="#" onclick="loadPage('')">Home</a> |
<a href="#calendar" onclick="loadPage('calendar')">Calendar</a> |
<a href="#contact" onclick="loadPage('contact')">Contact</a> |
</div>
<div id="content"></div>
</body>
</html>
你的工作。。。您不应该重定向客户端
这(有点像)谷歌地图的东西,对吧?你的意图是什么?
为什么不能用Ajax加载weather.php?我打赌你能。
相关文章:
- window.location替换并传递URL历史记录条目中的变量
- 可以<脚本类型=“;text/javascript”>window.location=“/"</
- window.location使用jquery mobile实现chrome跳转
- Windows.Open 在 window.Location 工作时不起作用
- 如何将所有作业自定义到window.location.href
- window.location未刷新到现有URL
- 如果window.location.href.indexOf('player=1')添加样式
- 在$state.go之后执行$window.location.reload(true)
- javascript window.location在检查firebug时给了我一个错误的url路径
- angularjs 中 $window.location.reload() 和 $route.reload() 之间的区
- 在AJAX中使用window.location.replace'成功'回调
- IE中不支持JQuery window.location.hash
- PhoneGap如何在更改window.location/document.location后保持Javascript的
- 如何在更改window.location后停止执行
- 警报中的window.location必须重定向到上一页
- 如何在窗体操作或window.location(PHP)中使用数据切换
- 正在将目标添加到window.location
- window.location.htm无法在chrome扩展中工作
- 可以'当window.location.href时,无法从javascript获取post值
- 基于AJAX的网站中的JS window.location