亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的 javascript 函數不能僅在 iOS Safari 中工作?

為什么我的 javascript 函數不能僅在 iOS Safari 中工作?

HUX布斯 2022-07-01 15:54:40
我正在使用 C# 和 ASP.NET Core 構建一個網站。其中一個網頁有一個按鈕,該按鈕應該使用 javascript 在輸入框中填寫您的地理位置。當我單擊按鈕時,我無法讓 iOS 中的 Safari 運行腳本。它似乎適用于我測試過的其他任何地方(Edge 上的 PC 桌面、Chrome、Firefox、Firefox 和 Chrome 中的 iOS 設備)。這是函數,它嵌入在按鈕所在的html頁面底部:<script>    var form = document.getElementById("locinput");    function getLocation() {        if (navigator.geolocation) {            navigator.geolocation.getCurrentPosition(prefillLocation);        } else {            x.innerHTML = "Geolocation is not supported by this browser.";        }    }    function prefillLocation(position) {        form.value = position.coords.latitude + ', ' + position.coords.longitude;    }</script>我嘗試以多種不同的方式將此功能附加到按鈕上。<input id="clickMe" type="button" value="Get GeoLoc" mousemove="getLocation();" onclick="getLocation();" /><button value="iosbutton" type="button" mousedown="getLocation();" onclick="getLocation();">iOS Button</button><a id="iosButton"class="btn" data-g-label="button1" href="javascript:getLocation();">Get Location</a><input type="button" value="Test" onmousedown="javscript:getLocation();" />在所有其他瀏覽器中,上述每個“按鈕”都有效。在 iOS 版 Safari 中,沒有任何反應。我通過用警報函數替換我的 getLocation() 函數進行了完整性檢查,如下所示:<input type="button" value="Test" onmousedown="javscript:alert('Hello');" />這在 iOS Safari 上運行良好。這讓我相信有某種內置的安全性不允許自定義腳本。但我確信有一種正確的方法可以做到這一點。有人可以幫我確定為什么會發生這種情況以及如何使我的自定義功能在 iOS 版 Safari 中工作嗎?
查看完整描述

1 回答

?
SMILET

TA貢獻1796條經驗 獲得超4個贊

好的,運行一些測試后的一些事情:

  1. 上面的“按鈕”沒有任何問題,Mac 和 iOS 上的 Safari 都可以正常響應onclick="function()"。

  2. geolocation如果頁面不是通過.Safari 訪問,Safari 將拒絕所有訪問https:。嘗試訪問geolocationwithhttp:將引發在控制臺中可見的錯誤,否則該錯誤將保持沉默。

  3. 如果 Safari 試圖通過 訪問geolocation,https瀏覽器會彈出一個對話框,上面寫著“網站 [your_site_name] 想使用您當前的位置?!?,帶有“不允許”和“允許”按鈕。

http://img1.sycdn.imooc.com//62bea9bc000120af08210261.jpg

如果用戶設法克服所有這些障礙,geolocation就會奏效。


測試代碼:


<html>

<head>

</head>

<body>

<form id="locinput">

  <input id="clickMe" type="button" value="Get GeoLoc"

         onclick="getLocation();"/>

  <button value="iosbutton" type="button" onclick="getLocation();">iOS

    Button

  </button>

  <a id="iosButton" class="btn" data-g-label="button1"

     href="#" onclick="getLocation();">Get Location</a>

  <input type="button" value="Test" onclick="getLocation();"/>

</form>

<script>

  const form = document.getElementById('locinput');


  function getLocation () {

    console.log('getLocation()');

    if (navigator.geolocation) {

      navigator.geolocation.getCurrentPosition(prefillLocation);

    } else {

      alert('Geolocation is not supported by this browser.');

    }

  }


  function prefillLocation (position) {

    form.value = position.coords.latitude + ', ' + position.coords.longitude;

  }

</script>

</body>

</html>


查看完整回答
反對 回復 2022-07-01
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號