如何使用javascript在新标签页中打开链接

时间:2022-12-07 01:27:27

I am working on a website, in which I have to open a url from backend. I am using c# now. My problem is that I want to open link in new tab instead of new window.

我正在一个网站上工作,我必须从后端打开一个网址。我现在正在使用c#。我的问题是我想在新标签而不是新窗口中打开链接。

My code is here:-

我的代码在这里: -

string url = ppHref.ToString();

string newScript = "<script language='javascript'>window.open('" + ppHref.ToString() + "', '_blank');</script>";

ClientScript.RegisterStartupScript(this.GetType(),"OpenUrl", newScript);

Can Anybody tell me how to open this url in new tab. I don't like pop ups so I don't wanna use window.open(). Please help me.

任何人都可以告诉我如何在新标签中打开此网址。我不喜欢弹出窗口,所以我不想使用window.open()。请帮帮我。

Thanks in Advance

提前致谢

5 个解决方案

#1


7  

<a href="javascript:" onclick="window.open('https://www.google.co.in');" target="_blank">Sample Code</a>

#2


13  

Have you try this code: Originally shared here

您是否尝试过此代码:最初在这里分享

function OpenInNewTab(url )
{
  var win=window.open(url, '_blank');
  win.focus();
}

In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers and the default "new window" behavior. You could do it this way or by adding an event listener to your DOM object.

在大多数情况下,这应该直接在链接的onclick处理程序中发生,以防止弹出窗口阻止程序和默认的“新窗口”行为。您可以通过这种方式或通过向DOM对象添加事件侦听器来实现。

<div onclick="OpenInNewTab('www.test.com');">Something To Click On</div>

Please also try this:

请试试这个:

Add your form name attribute like:

添加您的表单名称属性,如:

<form name="form">

and then try it like that:

然后尝试这样:

<a href="" onclick="window.open( form.url.value, 'windowName' ); return false" target="_blank">Submit</a>

and check this link http://jsfiddle.net/ef69K/

并查看此链接http://jsfiddle.net/ef69K/

#3


5  

You can use:

您可以使用:

window.open(url,'_target')

_target opens the page in next tab.

_target在下一个标签中打开页面。

#4


2  

Html Code :

Html代码:

<button onclick="OpenInNewTab();" type="submit">

Javascript Code :

Javascript代码:

function OpenInNewTab(url) {
    var win = window.open(url, '_blank');
    win.focus();
}

Change Your Browser Setting.

更改浏览器设置。

In FireFox ,

在FireFox中,

Go To Options -> Tab setting and Check "Open New Windows in a New Tab instead" setting.

转到选项 - >选项卡设置,然后选中“在新选项卡中打开新窗口”设置。

This Solution Worked For me .

这个解决方案适合我。

#5


0  

I think answer to this question will help , Open a URL in a new tab using JavaScript https://*.com/a/30512485/2293686

我认为回答这个问题会有所帮助,使用JavaScript在新标签页中打开一个URL https://*.com/a/30512485/2293686

try like this,

试试这样,

$('#myButton').click(function () {
    var redirectWindow = window.open('url', '_blank');
    redirectWindow.location;
});

#1


7  

<a href="javascript:" onclick="window.open('https://www.google.co.in');" target="_blank">Sample Code</a>

#2


13  

Have you try this code: Originally shared here

您是否尝试过此代码:最初在这里分享

function OpenInNewTab(url )
{
  var win=window.open(url, '_blank');
  win.focus();
}

In most cases, this should happen directly in the onclick handler for the link to prevent pop-up blockers and the default "new window" behavior. You could do it this way or by adding an event listener to your DOM object.

在大多数情况下,这应该直接在链接的onclick处理程序中发生,以防止弹出窗口阻止程序和默认的“新窗口”行为。您可以通过这种方式或通过向DOM对象添加事件侦听器来实现。

<div onclick="OpenInNewTab('www.test.com');">Something To Click On</div>

Please also try this:

请试试这个:

Add your form name attribute like:

添加您的表单名称属性,如:

<form name="form">

and then try it like that:

然后尝试这样:

<a href="" onclick="window.open( form.url.value, 'windowName' ); return false" target="_blank">Submit</a>

and check this link http://jsfiddle.net/ef69K/

并查看此链接http://jsfiddle.net/ef69K/

#3


5  

You can use:

您可以使用:

window.open(url,'_target')

_target opens the page in next tab.

_target在下一个标签中打开页面。

#4


2  

Html Code :

Html代码:

<button onclick="OpenInNewTab();" type="submit">

Javascript Code :

Javascript代码:

function OpenInNewTab(url) {
    var win = window.open(url, '_blank');
    win.focus();
}

Change Your Browser Setting.

更改浏览器设置。

In FireFox ,

在FireFox中,

Go To Options -> Tab setting and Check "Open New Windows in a New Tab instead" setting.

转到选项 - >选项卡设置,然后选中“在新选项卡中打开新窗口”设置。

This Solution Worked For me .

这个解决方案适合我。

#5


0  

I think answer to this question will help , Open a URL in a new tab using JavaScript https://*.com/a/30512485/2293686

我认为回答这个问题会有所帮助,使用JavaScript在新标签页中打开一个URL https://*.com/a/30512485/2293686

try like this,

试试这样,

$('#myButton').click(function () {
    var redirectWindow = window.open('url', '_blank');
    redirectWindow.location;
});