Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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(). Please help me.

Thanks in Advance

like image 340
yash Avatar asked Apr 16 '14 06:04

yash


People also ask

Can you make the link open in a new tab in JavaScript?

You need to use the _blank value in the target attribute to open the linked URL in a new tab or window. If you want to open URL with JavaScript, the open() method of Window interface is the best option. The JavaScript window. open() method opens a new browser window.

How do I open a link in a new tab?

You just need an anchor ( <a> ) element with three important attributes: The href attribute set to the URL of the page you want to link to. The target attribute set to _blank , which tells the browser to open the link in a new tab/window, depending on the browser's settings.

How do I open a new window in JavaScript?

The open() method opens a new browser window, or a new tab, depending on your browser settings and the parameter values.


2 Answers

<a href="javascript:" onclick="window.open('https://www.google.co.in');" target="_blank">Sample Code</a>
like image 153
Shaik Thameem Avatar answered Oct 03 '22 21:10

Shaik Thameem


You can use:

window.open(url,'_target')

_target opens the page in next tab.

like image 41
CodeOptimizer Avatar answered Oct 03 '22 23:10

CodeOptimizer