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

javascript - How to open a new browser tab from within a jQuery callback - Stack Overflow

programmeradmin1浏览0评论

I have tried the following to achieve this:

  • Jquery Open in new Tab (_blank)
  • How can I open a link in a new window?

But neither works when calling window.open from inside the callback. Here's my code

$.post('api', { 
    }, function() {
    var win = window.open('target-file', '_blank');
    win.focus();
    });

I have tried the following to achieve this:

  • Jquery Open in new Tab (_blank)
  • How can I open a link in a new window?

But neither works when calling window.open from inside the callback. Here's my code

$.post('api', { 
    }, function() {
    var win = window.open('target-file', '_blank');
    win.focus();
    });
Share Improve this question edited May 23, 2017 at 12:01 CommunityBot 11 silver badge asked May 10, 2013 at 10:09 coder9coder9 1,5491 gold badge28 silver badges52 bronze badges 2
  • 2 The new window may be blocked by a popup-blocker? – mariusnn Commented May 10, 2013 at 10:12
  • possible duplicate of Why can't I open a new window on my jquery ajax callback? – Adil Shaikh Commented May 10, 2013 at 10:13
Add a ment  | 

4 Answers 4

Reset to default 2

Try this:

 $.post('api', { 
    }, function() {
    window.open('target-file', '_blank');
   });

try it with the name of the window

Like

window.open("url", "NameOfNewWindow");

SEE HERE

use MySurfaceWindow = window.open("url","windowname","settings");
see below example:

MySurfaceWindow = window.open('/DesktopModules/DMS/DMS.PatientEChart/Surface Pages/Surface6.aspx?SurfaceTooth=' + $("#lbl" + $(this).val()).text() + '&ProcedureID=0' + '&ColorD=I' + '', '', 'height=240,width=200,top=' + top + ',left=' + left + 'status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=no');

ou can add this simple jQuery snipplet to your source to open every external link in a new tab of the web browser

$(document).ready(function(){
  $('a').each(function() {
    var a = new RegExp('/' + window.location.host + '/');
    if(!a.test(this.href)) {
      $(this).click(function(event) {
        event.preventDefault();
        event.stopPropagation();
        window.open(this.href, '_blank');
      });
    }
  });
});
发布评论

评论列表(0)

  1. 暂无评论