admin管理员组

文章数量:1406926

I'm trying to call window.open() inside loop x times (target=_blank) based on some criteria. But it opens new tab just one time. Is anything I'm doing wrong? Here is my code

$('#spogo_' + _sp.spid).button()
     .button('option', 
        {
             label : 'Go to',
             icons : {
                 primary : 'ui_icon_goto'
             }
        })
    .on('click', function(){
         var selRow = model._getSelectedRow($('#spt_' + this.id.split('spogo_')[1]).dataTable())[0];
         for(var i = 0 ; i < selRow.cells.length ; i ++){
             var cell = selRow.cells[i];
             if($(cell).hasClass('vdata_cell')){
                  window.open($(cell).attr('data'), '_blank');
             }
         }
    });

I've tried debug this and found that if happens multiple times, by the way during debug neither URL was opened.

EDIT: Aaahh sorry guys the popup was blocked :D But now one url opens inside new tab, another in new window, how can I resolve this?

I'm trying to call window.open() inside loop x times (target=_blank) based on some criteria. But it opens new tab just one time. Is anything I'm doing wrong? Here is my code

$('#spogo_' + _sp.spid).button()
     .button('option', 
        {
             label : 'Go to',
             icons : {
                 primary : 'ui_icon_goto'
             }
        })
    .on('click', function(){
         var selRow = model._getSelectedRow($('#spt_' + this.id.split('spogo_')[1]).dataTable())[0];
         for(var i = 0 ; i < selRow.cells.length ; i ++){
             var cell = selRow.cells[i];
             if($(cell).hasClass('vdata_cell')){
                  window.open($(cell).attr('data'), '_blank');
             }
         }
    });

I've tried debug this and found that if happens multiple times, by the way during debug neither URL was opened.

EDIT: Aaahh sorry guys the popup was blocked :D But now one url opens inside new tab, another in new window, how can I resolve this?

Share Improve this question edited Dec 16, 2013 at 19:14 Arsen Alexanyan asked Dec 16, 2013 at 19:01 Arsen AlexanyanArsen Alexanyan 3,1416 gold badges27 silver badges46 bronze badges 7
  • Why in the world are you looping and checking a class? Just use a selector to find the element. – epascarello Commented Dec 16, 2013 at 19:04
  • What is the value of $(cell).attr('data')? I just tried window.open('http://www.cheese.', '_blank') and it opens multiple tabs. – Mark Commented Dec 16, 2013 at 19:06
  • google. :) – Arsen Alexanyan Commented Dec 16, 2013 at 19:08
  • @Mark maybe the reason is that I'm calling inside click() event handler? – Arsen Alexanyan Commented Dec 16, 2013 at 19:10
  • 1 @ArsenAlexanyan Shouldn't make any difference, is your url fully formatted? – Mark Commented Dec 16, 2013 at 19:12
 |  Show 2 more ments

3 Answers 3

Reset to default 2

You can only specify the '_blank' and then the users browser is going to decide if it is in a new tab or a new window and you can not override that "feature".

You can't resolve this, it is a user preference in their browser for where they wish the link to be opened. It is also frowned upon from a usability standpoint to force users to open links in a new window

Check the spec for window.open. The way you have it written window.open(url, '_blank') is creating one window named _blank and opening the url in that same window each time. Try changing the name by appending a number from a counter or something.

As far as opening in a tab vs. a window - that is a preference in the browser, and you have no control over it.

本文标签: javascriptCall windowopen() inside loop during handling of element click eventStack Overflow