Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于AJAX的站点中的JS window.location_Javascript_Ajax_Geolocation - Fatal编程技术网

Javascript 基于AJAX的站点中的JS window.location

Javascript 基于AJAX的站点中的JS window.location,javascript,ajax,geolocation,Javascript,Ajax,Geolocation,我有一个简单的基于AJAX的站点,我有一个同样简单的地理定位功能,当用户单击按钮时,可以将其重定向到新页面 这是我的地理定位功能的一部分,它重定向用户,正如您将看到的,它将人们重定向到一个名为weather.php的页面 function showPosition(position) { window.location='weather.php?lat='+position.coords.latitude+'&long='+position.coords.longitude; }

我有一个简单的基于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.php50.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?我打赌你能