I'm trying to make it so that my button will open a url in a new tab.
I can get it to work using window.location.href, but the url doesn't open in a new tab that way.
I'm using jQuery and Javascript, and the code itself is inside of a larger jQuery function.
$('#code').click(function(){
window.location.href = ''
});
The button has no href attribute on page load.
<button id="code" type="submit" class="btn btn-success" style="width:400px;"><span class="glyphicon glyphicon-download"></span> Generate Code</button>
I've tried using .attr and .prop though it hasn't worked either. I'm kinda new to this stuff so I could've just done something wrong.
I'm open to either adding an href to the button or using the .click function or whatever, I just need to get this to open to a new tab.
Edit:: Ok, more explanation.
On Page load, the button onclick starts a timer that increments a progressbar. After the progressbar is done I want the button to open a link.
I'm trying to make it so that my button will open a url in a new tab.
I can get it to work using window.location.href, but the url doesn't open in a new tab that way.
I'm using jQuery and Javascript, and the code itself is inside of a larger jQuery function.
$('#code').click(function(){
window.location.href = 'http://www.example.com'
});
The button has no href attribute on page load.
<button id="code" type="submit" class="btn btn-success" style="width:400px;"><span class="glyphicon glyphicon-download"></span> Generate Code</button>
I've tried using .attr and .prop though it hasn't worked either. I'm kinda new to this stuff so I could've just done something wrong.
I'm open to either adding an href to the button or using the .click function or whatever, I just need to get this to open to a new tab.
Edit:: Ok, more explanation.
On Page load, the button onclick starts a timer that increments a progressbar. After the progressbar is done I want the button to open a link.
Share Improve this question edited Jan 23, 2014 at 10:34 codingrose 15.7k11 gold badges40 silver badges58 bronze badges asked Jan 22, 2014 at 21:11 EndingLegacyEndingLegacy 251 gold badge2 silver badges5 bronze badges 2 |2 Answers
Reset to default 15You could do this:
<a href="http://www.example.com" target="_blank" id="code" type="submit" class="btn btn-success" style="width:400px;"><span class="glyphicon glyphicon-download"></span> Generate Code</a>
I converted the button
to an a
tag and added target="_blank"
to open the link in a new tab.
This will open the url in a new window:
$('#code').click(function(){
window.open("http://www.w3schools.com");
});
http://www.w3schools.com/jsref/met_win_open.asp
btn
css class. Then just usetarget="_blank"
attribute. – Patrick Evans Commented Jan 22, 2014 at 21:15<a href="..." class="btn">
– rybo111 Commented Jan 22, 2014 at 21:24