开发者

Getting an Odd Error using Javascript/jQuery

Trying to select the following link in html:

<a href="/node/1742957/nodequeue">Nodequeue</a>

with this Javascript:

jQuery("a:contains('Nodequeue')").trigger("click");

And I am receiving this error message:

Javascript console (:1): Unsafe JavaScript attempt to access frame with URL http://cdn.nprove.com/cpma/p/1/2/e/b/12ebf3bc368ry3ra.html?npuid=1310010225&rurl=&id=cpma-2n7eypbvio581300288437193&null=&r=366424962878227 from frame with URL http://www.benzin开发者_如何学运维ga.com/analyst-ratings/analyst-color/11/07/1742957/the-beef-stops-here. Domains, protocols and ports must match.

Any idea what might cause this?


I created a JSFiddle of your code which you can look at and notice that in the console your error doesn't come up in Chrome 12 or FireFox 5. I'm not sure what version of jQuery you are using that is causing that or your DOM situation that may be triggering that error, however, try this potential fix:

(function(window, $) {

  $.fn.triggerAnchor = function() {
    return this.each(function(e) {
      var href = $(this).attr('href');
      window.location.href = href;

      return false;
    });
  };

})(this, this.jQuery);

Then use with:

$("a:contains('Nodequeue')").triggerAnchor();

I don't think jQuery triggers anchors, and it certaintly doesn't trigger native click events. This is the closest thing I can think of to emulate that behavior.

You can see it 'working' here

Explanation of the code:

The code is simply a jQuery plugin that looks at the href attribute of anchor and sets the window location to that value. I wrote in the typical pattern of a wrapped closure to localize references to window and jQuery. I'm allowing you to call this on multiple anchors, but I'm assuming the average user would only need to run this once.


That error usually means you are making a javascript request from one frame to another. In this case, is the link in an iframe, or is jquery running in an iframe?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜