第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

HUX布斯 2022-07-01 15:54:40
我正在使用 C# 和 ASP.NET Core 構(gòu)建一個網(wǎng)站。其中一個網(wǎng)頁有一個按鈕,該按鈕應(yīng)該使用 javascript 在輸入框中填寫您的地理位置。當(dāng)我單擊按鈕時,我無法讓 iOS 中的 Safari 運(yùn)行腳本。它似乎適用于我測試過的其他任何地方(Edge 上的 PC 桌面、Chrome、Firefox、Firefox 和 Chrome 中的 iOS 設(shè)備)。這是函數(shù),它嵌入在按鈕所在的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 中,沒有任何反應(yīng)。我通過用警報函數(shù)替換我的 getLocation() 函數(shù)進(jìn)行了完整性檢查,如下所示:<input type="button" value="Test" onmousedown="javscript:alert('Hello');" />這在 iOS Safari 上運(yùn)行良好。這讓我相信有某種內(nèi)置的安全性不允許自定義腳本。但我確信有一種正確的方法可以做到這一點(diǎn)。有人可以幫我確定為什么會發(fā)生這種情況以及如何使我的自定義功能在 iOS 版 Safari 中工作嗎?
查看完整描述

1 回答

?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個贊

好的,運(yùn)行一些測試后的一些事情:

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

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

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

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

如果用戶設(shè)法克服所有這些障礙,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>


查看完整回答
反對 回復(fù) 2022-07-01
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號