41

I have a web app that displays rows with go and delete buttons.

If a user clicks go, it should open new tab/window with url built from the row's data.

how can I do this in jquery? What I'm doing is :

$('.go').click( function () {
   var wid = {{ wid|tojson|safe }};
   var sid = $(this).prop('id');
   url = $script_root + '/' + wid + '/' + sid;

   // go to url
});

some update:

What I'm really tring to accomplish is dynamically update href of an <a> element.

<a id="foo" href="#">foo</a>
<script type="text/javascript>
$('#foo').click( function() {
  $(this).prop('href', 'http://www.google.com/');
});
</script>

which doesn't work (fiddle :http://jsfiddle.net/6eLjA/)

thkang
  • 11,215
  • 14
  • 67
  • 83

1 Answers1

92

Try this:

window.open(url, '_blank');

This will open in new tab (if your code is synchronous and in this case it is. in other case it would open a window)

karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • What do you mean by *if your code is synchronous*? – VisioN Mar 01 '13 at 08:58
  • 6
    @VisioN This code will not open new tab: $.get('someurl', function(){ window.open('anotherurl', '_blank'); }), it will open a window because ajax request is asynchronous by default (you can change it) – karaxuna Mar 01 '13 at 09:24
  • 1
    I'm getting an alert in google chrome telling that is has blocked a "Pop up". Any help with this ? Is there any way to by pass it ? – RPDeshaies May 15 '14 at 20:47
  • 3
    Tareck - window.open should be placed inside of a click event to prevent google chrome from blocking the pop up. – djrconcepts Aug 17 '14 at 23:58
  • Not too important, but could you surround 'url' in quotes? If you entered the URL parameter without quotes, it wouldn't run. – Blake Allen Aug 20 '16 at 06:47