2
<script type="text/javascript">
function ref() {
    location.href = 'http://www.google.com';
}
</script>

<a target="_blank" href="/somepage.html" onclick="javascript:ref()">CLICK ME</a>

In Chrome, FF and IE, clicking the link will open Google in the current window and open somepage.html in a new tab/window

But in Safari it will just open Google in the current window and nothing else.

How can I make Safari replicate the other browsers?

Gixxy22
  • 507
  • 1
  • 8
  • 20

1 Answers1

4

location.href will only change the URL of the current window. In order to open a new tab or window, its better to use window.open. I tested it in Safari specifically (and Chrome) and it is working in both browsers.

<a href="/somepage.html" onclick="javascript:ref()">CLICK ME</a>


<script type="text/javascript">
function ref() {
    window.open('http://www.google.com', '_blank');
}
</script>

The way you had it written, somepage.html will open in a new tab and the current page will redirect to google. If you want it that way just switch the URLs around like so:

<a href="www.google.com" onclick="javascript:ref()">CLICK ME</a>


<script type="text/javascript">
function ref() {
    window.open('/somepage.html', '_blank');
}
</script>

A side note, I found out that Safari allows user to handle how they want windows opening, whether it be a new window or a tab, and unfortunately we do not have any control over that.

maryjane
  • 171
  • 2
  • 10
  • thanks, but that wouldn't also open `/somepage.html`. I need to open a new tab and change the URL of the original tab all with 1 click. – Gixxy22 Jan 05 '15 at 18:20
  • @MikeMeade sorry about that. I updated my answer above. That should work as you need. – maryjane Jan 05 '15 at 18:37