2012-12-20 30 views
0

第二次更新:由於沒有修復了Safari和其他幾個奇怪的行爲,我決定建立使用這個選項(主要在腳本的Safari瀏覽器放棄)Safari用戶的所有新內容枯燥......什麼導致Safari無法獲取此代碼中的navigator.geolocation.getCurrentPosition?

<?php if($detect->version('Safari')) { ?> 
$(function() { 
    /* create second option because Safari sucks? */ 
    $("#gotorep").remove(); 
    $("#entersite").remove(); 
    $("#entersite2").css({'display':'inline'}); 
}); 
<?php } elseif($detect->isMobile()) { ?> 
    function startfunc() { 
     if(navigator.geolocation) { 
. 
. 
. 

錯誤更新(這是固定的,刪除):我在Safari中的錯誤是35772ReferenceError:找不到變量:在此位置失敗...

$(function() { 
    if(navigator && navigator.geolocation) { 
     var fallback = setTimeout(function() { fail('10 seconds expired'); }, 10000); 
***http://devv.referadiabetic.com/:35772ReferenceError: Can't find variable: fail 
     navigator.geolocation.getCurrentPosition(
       function (pos) { 
        clearTimeout(fallback); 

原文問題:我可以在除Safari之外的其他移動設備/瀏覽器中使用此工具。雖然在iphone上它失敗了,所以如果選擇與!並在具有相同問題的Safari桌面瀏覽器中進行測試,一旦我同意,就不會返回我的數據。這是代碼,我現在有工作了塊...

<?php if(!($detect->isMobile())) { ?> 
$(function() { 
    if(navigator && navigator.geolocation) { 
     var fallback = setTimeout(function() { fail('10 seconds expired'); }, 10000); 
     navigator.geolocation.getCurrentPosition(
       function (pos) { 
        clearTimeout(fallback); 
        var point = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
        new google.maps.Geocoder().geocode({'latLng': point}, function (res, status) { 
         if(status == google.maps.GeocoderStatus.OK && typeof res[0] !== 'undefined') { 
          var zip = res[0].formatted_address.match(/,\s\w{2}\s(\d{5})/); 
          if(zip) { 
           var zipcodepassed = zip[1]; 
           $("#em").html(zipcodepassed); 
           var reps = reps_load; 
           var ads_repList_URLtoFullName = ads_repList_URLtoFullName_load; 
           var rek = ads_repList_URLtoFullName; 
           var rep = reps[zipcodepassed]; 
            if (rep == undefined || rep == null || rep == "") { 
              $(function() { 
               $("#gotorep").remove(); 
               $("#entersite").remove(); 
               $("#entersite2").css({'display':'inline'}); 
              }); 
            } 
           var repname = rek[rep]; //i.e. shaner will be Shane Reaume in repname variable 
           $("#yourrep").html(repname); 
           $("a[href='http://devv.referadiabetic.com']").attr('href', 'http://devv.referadiabetic.com/' + rep); 
          } 
         } 
        }); 

       } 
     ); 

    } 
}); 

我已經有這個作爲一個回落,我使用的IP地址來獲得郵政編碼,但由於移動設備的特性,我正在設置這些代碼塊。

+0

它發生在桌面Safari上,對吧?在運行時,打開開發者工具並觀看控制檯,您可能會在那裏看到錯誤。 – Charles

+0

@Charles我在頂部分享了我的錯誤,現在正在研究這個問題,謝謝。 – Shane

回答

1

setTimeout期望第一個參數是一些代碼或調用函數的字符串。我認爲它試圖將fail解析爲一個字符串,並且...... erm ......失敗。

setTimeout("function() fail('10 seconds expired');", 10000); 
+0

根據[MDN](https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout),您應該可以直接將函數傳遞給setTimeout。代碼,正如你改變它,實際上只是返回一個函數......但它並不真正被返回到任何地方,所以它基本上是沒有操作的。這並不能阻止在setTimeout的上下文中沒有定義'失敗',當然這是潛在的問題。 – Charles

+1

我同意,但Safari肯定有'失敗'的問題。在這個時候,我通常會去看看這是不是一個保留字,但我已經知道它不是。 –

+0

@Charles我刪除了回退,現在我沒有收到錯誤,但它也不會返回該位置。 – Shane

相關問題