【问题标题】:Webshims, IE8, and GeolocationWebshims、IE8 和地理定位
【发布时间】:2011-12-07 18:07:16
【问题描述】:

我想要多个选项来为我们的 IE8 笔记本电脑进行地理定位,并且研究了 Google Chrome 框架(可以使用)和 jquery polyfill(我正在尝试使用它)。示例来自的网站是webshims,我已经验证上述演示页面在 IE8 中有效,但我无法为自己重新创建示例。这是我的jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
  <head>
    <meta charset=utf-8>
    <title>Geolocation</title>
    <script src="<c:url value="/js/jquery-1.6.4.min.js"/>" type="text/javascript"></script>
    <script src="<c:url value="/js/modernizr-latest.js"/>" type="text/javascript"></script>
    <script src="<c:url value="/js/polyfiller.js"/>" type="text/javascript"></script>
    <script type="text/javascript">
      //http://afarkas.github.com/webshim/demos/demos/geolocation.html
      $.webshims.polyfill();
      navigator.geolocation.getCurrentPosition(function(pos){ $("#status").html("found you! latitude: "+ pos.coords.latitude +"/longitude: " + pos.coords.longitude); });
    </script>
  </head>
  <body>
    <p>Finding your location: <span id="status">checking...</span></p>
  </body>
</html>

它适用于任何原生支持地理定位的浏览器,但 polyfill 似乎并没有为 IE8 完成它的工作。我收到以下错误:

网页错误详情

用户代理:Mozilla/4.0(兼容;MSIE 8.0;Windows NT 5.1; 三叉戟/4.0;铬框架/15.0.874.121; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) 时间戳:2011 年 12 月 7 日星期三 17:48:06 UTC

消息:“navigator.geolocation”为空或不是对象行:13 字符:7 代码:0 URI:../GeoLocation/jquery.action

我是否缺少使此功能正常运行的东西?

【问题讨论】:

    标签: jquery html internet-explorer-8 geolocation webshim


    【解决方案1】:

    您需要先激活 polyfill。请参阅头部包含以下脚本的示例源:

    $.webshims.setOptions('geolocation', {
                confirmText: '{location} wants to know your position. It is Ok to press Ok.'
            });
            //load all polyfill features
            //or load only a specific feature with $.webshims.polyfill('feature-name');
            $.webshims.polyfill();
    

    更新: 这个 HTML 页面在 IE9、IE7 和 Chrome 中都可以正常工作

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset=utf-8>
        <title>Geolocation</title>
        <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
        <script src="http://afarkas.github.com/webshim/demos/js-webshim/minified/extras/modernizr-custom.js" type="text/javascript"></script>
        <script src="http://afarkas.github.com/webshim/demos/js-webshim/minified/polyfiller.js" type="text/javascript"></script>
        <script type="text/javascript">
          //http://afarkas.github.com/webshim/demos/demos/geolocation.html
          $.webshims.setOptions('geolocation', {
                confirmText: '{location} wants to know your position. It is Ok to press Ok.'
            });
          $.webshims.polyfill();
          $(function(){
            navigator.geolocation.getCurrentPosition(function(pos){ $("#status").html("found you! latitude: "+ pos.coords.latitude +"/longitude: " + pos.coords.longitude); });
          });
        </script>
      </head>
      <body>
        <p>Finding your location: <span id="status">checking...</span></p>
      </body>
    </html>
    

    确保使用 HTTPL// 而不是 FILE://

    【讨论】:

    • 它给出了同样的错误,除了添加代码的第 17 行而不是第 13 行。
    • 我没有看到添加地理位置选项的行:$.webshims.setOptions('geolocation', {....});
    • 我没有更新我的帖子以反映添加它的变化,但我确实将它添加到我的本地来源,因此我的行号移动了。但它并没有解决问题。
    • 它必须是导致问题的下载文件,因为您是正确的,所以确实有效。我会从 github 重新下载它们,然后再试一次。
    【解决方案2】:

    我是 webshims 库的创建者。为了澄清您的问题,请在此处提供一些信息。

    1. 您无需设置选项即可进行地理定位工作。这只是一个选项,而不是初始化代码。 (更多信息设置options in general

    2. 是的,您必须初始化您的代码,如果您只需要 geoloaction 而不需要其他功能,您应该使用以下行(有关initialiazing features 的更多信息):

      $.webshims.polyfill('地理位置');

    如果您使用不带任何参数的 polyfill 方法,所有功能都将被实现,这会增加下载大小。

    1. Polyfill-Readiness(原因,您的脚本不起作用的原因)。 Webshims 使用脚本加载器实现这些功能,该加载器实现所有异步操作,因此您必须等待特定的回调。 (更多关于readyness of a feature的信息)

    要么等待 jQuery 的就绪回调(延迟到所有功能都实现:

    $(function(){
        //use feature here (all features + DOM are ready
    });
    

    或者,如果您想尽快访问您的功能,有一个特殊的就绪回调,将在 DOM-Ready 之前触发:

    $.webshims.ready('geolocation', function(){
        //use geolocation feature here
    });
    

    如果您有任何问题或建议,包括如何改进文档,请告诉我。

    【讨论】:

      【解决方案3】:

      也许 navigator.geolocation 不允许在本地主机上尝试将其推入 www。 :)

      【讨论】:

      • 那么,如果 localhost 不允许它,为什么它会在 Firefox、Chrome 和 IE9 中工作?
      猜你喜欢
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多