2

I am planning to open an external link ("www.google.com") in a "new tab of the same window" from within my asp.net project on button click. Is there a way to do it. (The links come from the database and looks like I cant do it in markup side).

Currently I tried , response.redirect & window.open but both don't seem to work.

Response.Redirect
 protected void btnSubmit_Click(object sender, EventArgs e)
        {
           Response.Redirect("www.google.com");
        }

Window.Open

 protected void btnSubmit_Click(object sender, EventArgs e)
      {   
string x = "www.google.com";

       string s = "window.open('" + x + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";

        ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
  }

The error I get in both cases is : 404 page not found..

enter image description here

CodeNinja
  • 3,188
  • 19
  • 69
  • 112
  • 1
    Did you notice the error message... the 'Requested URL' shows the reason for the issue. Is it okay to use jquery in your application? In my project, I am using a jquery based solution to achieve similar functionality. – JGV Sep 06 '15 at 19:33
  • Yes ! I can use jquery .. Please let me know how I can achieve it in jquery. The error was because I should use "http://www.google.com" instead of "www.google.com". Also, I need to open the new url in a new tab. Response.redirect opens the new url in the same window. – CodeNinja Sep 06 '15 at 21:27

5 Answers5

2

Notice the URL being requested:

http://localhost/www.google.com

If you're directing the user to another site, you have to fully-qualify the URL:

Response.Redirect("http://www.google.com");

or:

string x = "http://www.google.com";

Otherwise the browser thinks it's looking for a resource called "www.google.com" on the current site.

David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks that works partially, but as I mentioned in my question I want to open the new url as a "new tab of the same window". How can I do that ? The above solution opens the url as a new window. – CodeNinja Sep 06 '15 at 21:34
  • @CodeNinja: The JavaScript `window.open()` should do exactly that. Is that not working in your browser? – David Sep 06 '15 at 21:37
  • The window.open() is opening in a new window instead of a new tab on the same window. I am using internet explorer. The code is below. string x = "https://www.google.com"; string s = "window.open('" + x + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');"; ClientScript.RegisterStartupScript(this.GetType(), "script", s, true); – CodeNinja Sep 06 '15 at 21:43
  • @CodeNinja: It's up to the browser how to open the new window object, whether it's a tab or a separate window. The code can't control that: http://stackoverflow.com/questions/4907843/open-a-url-in-a-new-tab-using-javascript – David Sep 06 '15 at 21:46
2

What you can do is send a chunk of html down the response that has a link with your url as an href, target="_blank" and a chunk of javascript onload of the form that fakes a click. If this doesn't work then use a window.open("www.google.com");

response.write("<script>");
response.write("window.open('www.google.com','_blank')");
response.write("</script>"); 

Full description here

Community
  • 1
  • 1
Krishna shidnekoppa
  • 1,011
  • 13
  • 20
0

You have to specify the full URL, so use

 Response.Redirect("Http://www.google.com");

Goodluck.

Slashy
  • 1,841
  • 3
  • 23
  • 42
  • Hey Slashy ! Thanks ! But the above solution opens the url "www.google.com" in the same window. As I mentioned in my question, I would like to open the new link "as a new tab in the same window". How can I do that ? – CodeNinja Sep 06 '15 at 21:31
0

Try:

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string x = "https://www.google.com";

        string s = "window.open('" + x + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";

        ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
    }

Edit 2:

According to This, you can't open it in new window in this way.

if you really want it in a new Window then use HTML Links with target="_blank" attribute.

Community
  • 1
  • 1
A_Sk
  • 4,532
  • 3
  • 27
  • 51
  • Thanks that works partially, but as I mentioned in my question I want to open the new url as a "new tab of the same window". How can I do that ? The above solution opens the url as a new window. – CodeNinja Sep 06 '15 at 21:30
0

The URL www.google.com is interpreted as local path, try

http://www.google.com

instead.

Using _new as window name opens a new window or tab:

window.open("...", "_new") 
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63