0

This used to be my (working) code:

<div onclick="location.href='http://somewebsite.com/';">
    <a href="http://someotherwebsite.com/">Link</a>
</div>

If I'd click the link, I would be taken to http://someotherwebsite.com/. If I'd click somewhere else in the div, I would be taken to http://somewebsite.com/. Perfect.

Then, I decided that I wanted to open http://someotherwebsite.com/ in a new tab upon clicking the link, so I changed my code to this:

<div onclick="location.href='http://somewebsite.com/';">
    <a href="http://someotherwebsite.com/" target="_blank">Link</a>
</div>

However, if I'd click the link now, I would be taken to http://somewebsite.com/ instead of http://someotherwebsite.com/! Apparently, adding target="_blank" to my anchor tag caused my onclick method to override my anchor tag's href. What's going on?

Edit: It turns out, my code does function properly in Chrome, but not in Safari 7.0.5. What can I do to add Safari support?

Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63

1 Answers1

0

Try

<div onclick="location.href='http://somewebsite.com/';" target="_self">
    <a href="http://someotherwebsite.com/" target="_blank">Link</a>
</div>

jsfiddle http://jsfiddle.net/guest271314/8deea/

guest271314
  • 1
  • 15
  • 104
  • 177
  • That did not change the behaviour, unfortunately. :( Clicking the link still opens http://somewebsite.com/ and not http://someotherwebsite.com/. – Tim Vermeulen Jul 27 '14 at 01:03
  • Interesting . Composed a basic `html` page with this content ` ` , tested locally , as try at jsfiddle SO has same origin policy (see `console` at jsfiddle). Worked ok at basic html page . `css` issue ? Try above `html` in file , i.e.g., `link.html` , without style, clicking both around `link` and `link` itself ? Thanks – guest271314 Jul 27 '14 at 01:07
  • Apparently, this is a browser problem... Your code (and mine, too) indeed works in Chrome (and Opera), but not in Safari 7.0.5. That's inconvenient. Thanks for the help, man. By the way, when I opened your file in Chrome and clicked the link, a new tab opened http://api.jquery.com/, but the original tab did still open http://stackoverflow.com/. Any idea how I can prevent the latter? – Tim Vermeulen Jul 27 '14 at 01:19
  • For clarity , which link should remain at same tab ? `div` or `a` ? See also https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a at "target" , http://stackoverflow.com/questions/4964130/target-blank-vs-target-new. Thanks – guest271314 Jul 27 '14 at 01:23
  • `div` should link to the same tab when clicked. However, when `a` is clicked, then a new tab should open while the original tab should stay on the same webpage. – Tim Vermeulen Jul 27 '14 at 01:32
  • That should be current composition at post . If goal is for safari compatibility , perhaps utilize jquery `.on()` , `.off()`, `e.preventDefault()`, `e.stopPropagation()` ? Thanks – guest271314 Jul 27 '14 at 01:52