【问题标题】:How to open a link in new tab using javascript/jQuery如何使用 javascript/jQuery 在新选项卡中打开链接
【发布时间】:2015-03-08 12:31:11
【问题描述】:

我想在新标签中打开url

我正在使用以下代码在新标签中打开网址。

var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
window.open(url, '_blank'); //exception here

但是通过 Uncaught TypeError: Cannot read property 'open' of undefined 例外,这对我不起作用

我也使用了以下。

var url = "/Billing/DownloadEDIForMultipleClaim?month=" + month + "&BillRequestIds=" + billRequestIds.join(',') + "&PatientIDs=" + patientIds.join(',');
var win = window.open(url, '_blank');
if(win) {
    //Browser has allowed it to be opened
    win.focus();
} else {
    //Broswer has blocked it
    alert('Please allow popups for this site');
}

我已经看过相关问题了。

How to open a URL in a new Tab using javascript or jquery?

How to open a link in new tab using javascript

但这对我不起作用。

【问题讨论】:

  • 所以基本上消息的意思是 window 未定义,这似乎不太可能?
  • open is undefined' when I use 'window.location.href' then exception occur location 未定义`
  • 在浏览器内的标准javascript实现中,window对象应该被定义为当前视图表面。因此,您可能遇到了浏览器问题,或者您的代码可能重载了该对象。
  • @DanielPersson 我怀疑window 对象是否重载,因为这不会给出此错误Uncaught TypeError: Cannot read property 'open' of undefined
  • 好吧,重载是错误的措辞:)。已更改、覆盖或未定义。

标签: javascript jquery tabs browser-tab


【解决方案1】:

这可能有用: http://jsfiddle.net/vraCM/12/ 看来这个代码 sn-p 包含了你需要的东西。

$(function() {
   $("a[href^='http']:not([href*='jsfiddle.net'])").each(function() {
       $(this).click(function(event) {
             event.preventDefault();
             event.stopPropagation();
             window.open(this.href, '_blank');
        }).addClass('externalLink');
   });
});

js fiddle 包含的代码可以被操纵以适应您在警报或新窗口等中弹出窗口的参数。

另外,这篇文章可能值得一读。 http://www.jqueryfaqs.com/Articles/Open-new-tab-in-browser-using-jQuery.aspx

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多