简体   繁体   English

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

[英]How to open a link in new tab using javascript

I am working on a website, in which I have to open a url from backend.我在一个网站上工作,我必须从后端打开一个网址。 I am using c# now.我现在正在使用 c#。 My problem is that I want to open link in new tab instead of new window.我的问题是我想在新选项卡而不是新窗口中打开链接。

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() .我不喜欢弹出窗口,所以我不想使用window.open() Please help me.请帮我。

Thanks in Advance提前致谢

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. 在大多数情况下,这应该直接在链接的onclick处理程序中发生,以防止弹出窗口阻止程序和默认的“新窗口”行为。 You could do it this way or by adding an event listener to your DOM object. 您可以通过这种方式或通过向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/

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

You can use:您可以使用:

window.open(url,'_target')

_target opens the page in next tab. _target在下一个选项卡中打开页面。

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 ,火狐中

Go To Options -> Tab setting and Check " Open New Windows in a New Tab instead " setting.转到选项->选项卡设置并选中在新选项卡中打开新窗口”设置。

This Solution Worked For me .这个解决方案对我有用。

I think answer to this question will help , Open a URL in a new tab using JavaScript https://stackoverflow.com/a/30512485/2293686我认为这个问题的答案会有所帮助,使用 JavaScript 在新选项卡中打开一个 URL https://stackoverflow.com/a/30512485/2293686

try like this,试试这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM