Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practice for JS - window.open() in href or in onclick?

Just a question about optimization, between :

<a href="#" onClick="javascript:window.open('myUrl.com');">link-1</a>

and :

<a href="javascript:window.open('myUrlBis.com');">link-2</a>

Is one better than the other ? Or more compatible ? Thanks.

like image 901
4wk_ Avatar asked Nov 30 '22 23:11

4wk_


1 Answers

Best practice is to use the target attribute:

<a href="http://myUrl.com" target="_blank">link-1</a>

If that doesn't suit, a click handler (ideally not assigned via attribute) would be my take.

like image 175
T.J. Crowder Avatar answered Dec 14 '22 13:12

T.J. Crowder