Skip to content

テクニック SCR24:利用者の要求に応じて新しいウィンドウを開くために、プログレッシブエンハンスメントを使用する

このテクニックについて

このテクニックは 3.2.5: 要求による変化 (ポップアップウィンドウを含むために用いる場合、十分なテクニック) に関連する。

このテクニックは、HTML に適用される。

解説

このテクニックの目的は、利用者が要求していない新しいウィンドウの出現によって引き起こされうる混乱を回避することである。突然新しいウィンドウが開くと、利用者は混乱したり、そのことに気づかなかったりする。 新しいウィンドウ/タブは HTML の target 属性又は JavaScript で開くことができる。以下にある事例は、スクリプトを用いて新しいウィンドウを開く方法を示している。その事例では、リンクにイベントハンドラを追加して、利用者にリンク先のコンテンツが新しいウィンドウで開くことを事前に知らせている。

事例

事例 1: JavaScript を使用して新しいタブ/ウィンドウを開く

マークアップ:

スクリプトはドキュメントの head 要素内に組み込まれており、リンクにはスクリプトのフックとなる id 属性がある。

<script src="popup.js"></script>
...
<a href="help.html" id="newwin">Show Help</a>

スクリプト:

window.onload = addHandlers;

function addHandlers(){
  var objAnchor = document.getElementById('newwin');
  
  if (objAnchor){
    objAnchor.firstChild.data = objAnchor.firstChild.data + ' (opens in a new window)';
    objAnchor.onclick = function(event){return launchWindow(this, event);}
    // UAAG requires that user agents handle events in a device-independent manner
    // but only some browsers do this, so add keyboard event to be sure
    objAnchor.onkeypress = function(event){return launchWindow(this, event);}
  }
}
   
   function launchWindow(objAnchor, objEvent)
   {
     var iKeyCode, bSuccess=false;
   
     // If the event is from a keyboard, we only want to open the
     // new window if the user requested the link (return or space)
     if (objEvent && objEvent.type == 'keypress')
     {
       if (objEvent.keyCode)
         iKeyCode = objEvent.keyCode;
       else if (objEvent.which)
         iKeyCode = objEvent.which;
   
       // If not carriage return or space, return true so that the user agent
       // continues to process the action
       if (iKeyCode != 13 && iKeyCode != 32)
         return true;
     }
   
     bSuccess = window.open(objAnchor.href);
   
     // If the window did not open, allow the browser to continue the default
     // action of opening in the same window
     if (!bSuccess)
       return true;
   
     // The window was opened, so stop the browser processing further
     return false;
   }

関連リソース

推奨を意味するものではない。

検証

手順

  1. ドキュメントにあるリンクを起動して、新しいウィンドウが開くかどうかをチェックする。
  2. 新しいウィンドウを開くリンクごとに、次をそれぞれ達成するためにスクリプトを用いていることをチェックする:

    1. リンクが新しいウィンドウを開くことを明示している
    2. デバイス非依存のイベントハンドラを用いている、そして
    3. 新しいウィンドウを開けない場合、ブラウザが同じウィンドウにリンク先のコンテンツを開くことができる。

期待される結果

  • 2 が真である。
Back to Top