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/)

3
  • isn't it jQuery code.? Commented Mar 1, 2013 at 8:51
  • you can provide "_blank" while calling window.open Commented Mar 1, 2013 at 8:52
  • Visit stackoverflow.com/questions/7554108/… Commented Mar 1, 2013 at 8:55

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

5 Comments

What do you mean by if your code is synchronous?
@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)
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 ?
Tareck - window.open should be placed inside of a click event to prevent google chrome from blocking the pop up.
Not too important, but could you surround 'url' in quotes? If you entered the URL parameter without quotes, it wouldn't run.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.