最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding href attribute to bootstrap button - Stack Overflow

programmeradmin0浏览0评论

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 why not just make an anchor tag a button by using the btn css class. Then just use target="_blank" attribute. – Patrick Evans Commented Jan 22, 2014 at 21:15
  • 4 JavaScript is a great tool, but you should avoid it where it's not necessary. Use <a href="..." class="btn"> – rybo111 Commented Jan 22, 2014 at 21:24
Add a comment  | 

2 Answers 2

Reset to default 15

You 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

发布评论

评论列表(0)

  1. 暂无评论