Frage

i have a link with a click handler here. the href is filled with information i need. but i dont want the click to open this link. so i did event.preventDefault and it works fine. But i need to target="_top" ich someone clicks the link. is this even possible?

War es hilfreich?

Lösung 2

changed the event.preventDefault(); function to return false; and did a html animatation with scrollTop: $('html, body').animate({scrollTop:0});

now the link is not following the href but it scrolls to top

Andere Tipps

If you're preventing the default action of the link, I take it you're doing something else. If that something else involves changing the window location, and you want to change the location of the top window, you can use window.top to refer to it and then set its location via the location property, e.g.:

window.top.location = /* ... */;

For instance (live example):

HTML:

<a id="theLink" href="http://jsbin.com">This link</a>
says jsbin.com and doesn't have <code>target="_top"</code>,
but will actually go to stackoverflow.com and do it
in the top window. (Seems a bit evil.)

JavaScript:

jQuery(function($) {

  $("#theLink").click(function(event) {
      window.top.location = "http://stackoverflow.com";
      return false;
  });

});

...but this raises the question of why not just let the link do its job. :-)

Or if you're trying to add target="_top" to a link that doesn't have it when it's clicked, you can do:

var link = /* ...get the link element, e.g., $("#theLink") or whatever */;
$("#theLink").click(function() {
    link.attr("target", "_top");
});

Note that I'm not preventing the default action there.

I wouldn't want to guarantee that would necessarily work, though — better to set the attribute before they click the link.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top