【问题标题】:Javascript/jQuery: programmatically follow a linkJavascript/jQuery:以编程方式跟随链接
【发布时间】:2013-03-16 08:49:40
【问题描述】:

在 Javascript 代码中,我想以编程方式使浏览器跟随我页面上的链接。简单案例:

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

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

这是假设的,因为它实际上不起作用。不,触发click 不会这样做。

我知道window.locationwindow.open,但它们在某些对我很重要的方面与原生链接跟踪不同:a)在存在&lt;base /&gt; 元素的情况下,b)在@ 的情况下987654327@ 网址。后者尤其重要。至少在 Firefox 中,调用 window.location.href = "mailto:somebody@example.com" 会导致窗口的 unload 处理程序触发,而据我所知,仅单击 mailto 链接不会。

我正在寻找一种方法来触发浏览器对链接的默认处理,从 Javascript 代码。

这样的机制存在吗?也欢迎提供工具包特定的答案(尤其是 Gecko)。

【问题讨论】:

  • 为什么不在你的 mailto: 链接中使用 target="_blank" 属性?
  • 我可以,但这会留下一个空白窗口打开,而没有那个我的邮件客户端打开但我的浏览器保持原样,这就是我想要的。
  • 您的 jsFiddle 示例适用于 Firefox Aurora 14.0a2 (2012-06-04)。

标签: javascript html hyperlink


【解决方案1】:

据我所知,window.location 完全符合您的要求,触发了浏览器的默认链接点击行为。

一些浏览器会在任何事件被触发或实际的 href 改变之前注意到协议。

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

尝试下面提到的小提琴演示,我得到以下结果:

  • Chromium:使用按钮和链接触发 onbeforeunload
  • Firefox 仅针对按钮触发 onbeforeunload
  • Safari:从不触发onbeforeunload
  • Opera:与 Safari 相同

所以防止unload 事件被触发的一个好方法是在beforeunload 中返回false。

【讨论】:

  • 不,不太一样。见jsfiddle.net/eyS6x/2。 “点击我”调用beforeunload 处理程序,链接本身不会。
  • 很公平,我现在在多个浏览器中对此进行了测试,似乎行为完全不同。我会更新我的答案以反映这一点。
  • 好吧,你可能在这里有所收获。在我的真实代码中,它是 only beforeunload 而不是 unload。我在我的OP中搞砸了。有没有办法我的beforeunload 处理程序中检测它是否是mailto?
  • OP 不是在问如何模仿链接行为,而是在问如何产生与单击链接完全相同的行为。即使它没有触发onbeforeunload,它仍然是错误的,因为它不会触发链接上的click 事件。
  • 不,实际上OP是专门询问如何“触发浏览器对链接的默认处理”。在我看来,这并不意味着链接的实际点击,而主要是点击之后发生的事情,例如跟踪 URL 或触发相关事件。
【解决方案2】:

方法一点击方法

HTMLElements有一个方法click()https://developer.mozilla.org/en/DOM/element.click

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

方法 2 触发合成事件

我想知道为什么 saluce 删除了他的答案。该解决方案是我过去使用过的(当点击只是 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');
}

【讨论】:

  • 确实没有。试试jsfiddle.net/eyS6x/1 并点击“点击我”。这是用于 MacOS 的 Firefox 5.0。
  • 它适用于 Windows 上的 Chrome (19.0.1084.52 m)、Firefox (10.0.3) 和 IE (8.0.7600)。可能是 Mac 问题。
  • 可能是这样。我会再测试几个地方。
  • @Dan 试试我发布的新解决方案,我没有mac来测试它
  • 好吧,我从你的帖子中学到了一些新东西,所以为此 +1,但不幸的是它不起作用,至少在 jsFiddle 中。 Chrome 抛出错误“拒绝显示文档,因为 X-Frame-Options 禁止显示。” Firefox 5 似乎什么也没做。
猜你喜欢
  • 1970-01-01
  • 2016-04-03
  • 2012-02-23
  • 2015-09-05
  • 1970-01-01
  • 2018-12-02
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多