IOS不支援iframe的解決辦法

來源:互聯網
上載者:User

標籤:支援   代碼   element   index   har   center   cal   位置   comm   

ios 不支援 iframe 的完美解決方案最近做公眾號頁面,因為入口不同,需要把公用頁面抽取出來,然後根據不同的值,傳遞給後台不同的入口。剛開始用iframe頁面嵌套(第一次嘗試使用),但發現iOS系統對iframe嵌套頁面的高度和定位控制的不到位,當嵌套的子頁面的高度大於父頁面的高度,且子頁面中有觸發彈框事件時,這時,如果子頁面高度遠遠大於父頁面高度,就會出現彈框找不到的情況,其實可能是在視口以下,彈框的位置只是相對於子頁面來定位,並沒有相對於視口定位。

嘗試了好多種方法,均不理想,而安卓系統則表現良好,於是想到,iOS系統可直接做頁面跳轉,而安卓系統則正常使用iframe(如果安卓系統直接做頁面跳轉,會有問題,故使用iframe),

直接看代碼:

<!DOCTYPE html>

<html lang="en">

 <head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">

</head>

 <body>

  <script src="js/zepto.min.js"></script>

  <script>

  $(function() {

    var originId = sessionStorage.setItem(‘originId‘, ‘3‘); //originId為後台需要判斷是哪個入口的值

    var u = navigator.userAgent;

    var isAndroid = u.indexOf(‘Android‘) > -1 || u.indexOf(‘Adr‘) > -1; //android終端

    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端

    if (isAndroid) {

      //安卓終端使用iframe

      var winH = $(window).height();

      var iframe = document.createElement(‘iframe‘);

      iframe.src = "index_common.html";

      iframe.style.width = ‘100%‘;

      iframe.style.height = winH + ‘px‘;

      iframe.style.border = ‘0 none‘;

      iframe.setAttribute(‘scrolling‘, ‘auto‘);

      document.body.appendChild(iframe);

    } else if (isiOS) {

      //iOS終端直接頁面跳轉

      location.href = ‘index_common.html‘;

    } else {

      location.href = ‘index_common.html‘;

    }

  })

 </script>

</body>

</html>

 

IOS不支援iframe的解決辦法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.