4

Objective

I want to open a URL in a new tab/window in the EXACT same manner as target="_blank" would.

Code

I'm using a PHP condition which triggers the following JavaScript:

<script type="text/javascript">
window.open ("http://www.google.com/","_blank", "status=1,toolbar=1");
</script>

My problem

window.open is NOT THE SAME as target="_blank" hyperlinks.

  1. It presents an issue with pop-up blockers.
  2. The window requires parameters to look like what target="_blank" would have produced.
  3. Once the JavaScript runs, certain font colors of the containing document are lost.

My question

How do I EXACTLY simulate what's produced by target="_blank"?

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
Dominor Novus
  • 2,248
  • 5
  • 29
  • 35
  • 3
    You don't. Pop-up blockers check now-a-days to make sure that the action is _user initiated_ otherwise it will block it. – LoveAndCoding Jan 25 '12 at 00:16
  • Possible duplicate: http://stackoverflow.com/questions/1574008/how-to-simulate-target-blank-in-javascript – Travis J Jan 25 '12 at 00:16
  • @Ktash: What will it take for code to become 'user initiated'? Would it make sense to simulate the .click event? – Dominor Novus Jan 25 '12 at 01:09
  • @Travis J: Thanks but I've already seen that question. It doesn't account for the first difference I outlined. – Dominor Novus Jan 25 '12 at 01:10
  • Simulating the click won't work (that I know of at least). You would have to trigger once the user _actually_ clicks. You won't be able to make code 'user initiated'. Unless you could 'trick' the user into click, or moving their mouse (not sure if this will work), or something. – LoveAndCoding Jan 25 '12 at 01:15
  • @Ktash: Depending on a choice of radio buttons, the user clicks a submit button which triggers the PHP validation that produces the JavaScript open/redirect. Can something be done here? – Dominor Novus Jan 25 '12 at 09:14
  • See a similar question posted a few days ago: http://stackoverflow.com/q/8978865/422184 and one posted a while ago: http://stackoverflow.com/a/1086733/422184 – LoveAndCoding Jan 25 '12 at 15:25

1 Answers1

1

Instead of opening the Window from JavaScript, use JavaScript to update a link's href and then trigger a click on the link. This way you'll get the exact same behavior as the user clicking the link.

Add a link to your page with an id and target="_blank". When you want to open a new window, update the href of this link and then trigger a click, like this (from here).

function clickLink(link) {
var cancelled = false;

if (document.createEvent) {
    var event = document.createEvent("MouseEvents");
    event.initMouseEvent("click", true, true, window,
        0, 0, 0, 0, 0,
        false, false, false, false,
        0, null);
    cancelled = !link.dispatchEvent(event);
}
else if (link.fireEvent) {
    cancelled = !link.fireEvent("onclick");
}

if (!cancelled) {
    window.location = link.href;
}
}
Community
  • 1
  • 1
Samuel Neff
  • 73,278
  • 17
  • 138
  • 182