18

Javascriptコードでは、プログラムでブラウザに自分のページにあるリンクをたどらせたいと思います。単純なケース:

<a id="foo" href="mailto:somebody@example.com">something</a>

function goToBar() {
   $('#foo').trigger('follow');
}

実際には機能しないため、これは架空のものです。いいえ、トリガーはそれをclick行いません。

私は知ってwindow.locationwindow.openますが、これらはネイティブリンクとは異なります-私にとって重要ないくつかの点でフォローしています:a)<base />要素が存在する場合、およびb)mailtoURLの場合。特に後者は重要です。少なくともFirefoxでは、呼び出すwindow.location.href = "mailto:somebody@example.com"とウィンドウのunloadハンドラーが起動しますがmailto、私が知る限り、リンクをクリックするだけでは起動しません。

Javascriptコードから、ブラウザのデフォルトのリンク処理をトリガーする方法を探しています。

そのようなメカニズムは存在しますか?ツールキット固有の回答も歓迎します(特にGeckoの場合)。

4

2 に答える 2

28

私の知る限り、window.locationはあなたが探しているものを正確に実行し、ブラウザのデフォルトのリンククリック動作をトリガーします。

一部のブラウザは、イベントが発生する前、または実際のhrefが変更される前にプロトコルに気づきます。

window.location = "mailto:somebody@example.com";

下記のフィドルデモを試してみると、次の結果が得られます。

  • クロム:onbeforeunloadボタンとリンクで発火
  • Firefoxonbeforeunloadはボタンに対してのみ起動します
  • Safari:発火しないonbeforeunload
  • Opera:Safariと同じ

したがって、unloadイベントが発生するのを防ぐ良い方法は、でfalseを返すことですbeforeunload

于 2012-06-05T23:18:03.910 に答える
6

METHOD 1クリック方式

HTMLElements メソッドがありclick() ます https://developer.mozilla.org/en/DOM/element.click

function goToBar() {
   document.getElementById('foo').click();
}

方法 2合成イベントの起動

なぜサルースが彼の答えを削除したのだろうか。その解決策は、私が過去に使用したものです(クリックがIEのみのものだったとき)。つまり、合成ブラウザ イベントを発生させます (jQuery のような偽物ではありませんclick())。そのアイデアを使用して解決策を投稿させてください...

デモ: http://jsfiddle.net/eyS6x/3/

/**
 * Fire an event handler to the specified node. Event handlers can detect that the event was fired programatically
 * by testing for a 'synthetic=true' property on the event object
 * @param {HTMLNode} node The node to fire the event handler on.
 * @param {String} eventName The name of the event without the "on" (e.g., "focus")
 */
function fireEvent(node, eventName) {
  // Make sure we use the ownerDocument from the provided node to avoid cross-window problems
  var doc;
  if (node.ownerDocument) {
    doc = node.ownerDocument;
  } else if (node.nodeType == 9 /** DOCUMENT_NODE */){
    // the node may be the document itself
    doc = node;
  } else {
    throw new Error("Invalid node passed to fireEvent: " + +node.tagName + "#" + node.id);
  }

  if (node.fireEvent) {
    // IE-style
    var event = doc.createEventObject();
    event.synthetic = true; // allow detection of synthetic events
    node.fireEvent("on" + eventName, event);
  } else if (node.dispatchEvent) {
    // Gecko-style approach is much more difficult.
    var eventClass = "";

    // Different events have different event classes.
    // If this switch statement can't map an eventName to an eventClass,
    // the event firing is going to fail.
    switch (eventName) {
      case "click":
      case "mousedown":
      case "mouseup":
        eventClass = "MouseEvents";
        break;

      case "focus":
      case "change":
      case "blur":
      case "select":
        eventClass = "HTMLEvents";
        break;

      default:
        throw "JSUtil.fireEvent: Couldn't find an event class for event '" + eventName + "'.";
        break;
    }
    var event = doc.createEvent(eventClass);
    var bubbles = eventName == "change" ? false : true;  
    event.initEvent(eventName, bubbles, true); // All events created as bubbling and cancelable.

    event.synthetic = true; // allow detection of synthetic events
    node.dispatchEvent(event);
  }
};

document.getElementById('button').onclick = function() {
   fireEvent( document.getElementById('link'), 'click');
}
于 2012-06-05T23:03:42.753 に答える