1
1

More than 3 years have passed since last update.

iPhoneのSafariで新しいタブを開いた際にそのタブの戻るボタンを無効化する方法

Last updated at Posted at 2019-12-15

はじめに

  • iPhone ではアンカータグをタップして新しいタブを開いた後、左から右にスワイプ又は戻るボタンをタップすることで、開いているタブを閉じて前のページへ戻ることができる。
  • 開く新しいタブの戻るボタンを無効化する

  • iPhone(11.4.1) の Safari のみ確認済

コード

  • 戻ることができる条件は、新しいタブを開いた後、前のタブのURLが変化していない場合。
  • つまり、タブを開く -> 元のタブのURLを変更 とする必要がある。
<a href="/hoge/foo.html" target="_blank" class="anti_back">CLICK</a>


$(function () {
    $('.anti_back').click(function(){
        window.open($(this).prop('href'));
        location.href = "#" + getRandomString(6);
        return false;
    });
});

function getRandomString(num){
    var C = "abcdefghijklmnopqrstuvwxyz";
    var str = "";
    for (var i = 0; i < num; i++) {
        str += C[Math.floor(Math.random() * C.length)];
    }
    return str;
}

  • アンカータグのクリック拾ってwindow.openで新しいタブを開いた後にURLに適当な文字のURLフラグメント付与
  • アンカータグ自体は return false で無効

さいごに

  • 自分はこれしか思いつかなかった。外のサイトはどうやってるんだろう...
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1