1

I know there are a lot of these questions out there. However, the problem I am running into is that I am building out the javascript and/or jquery (tried both) from a C# page that creates a hyperlink, uses ScriptManager to register a startup script and triggers the click event.

The reason I want to do this instead of redirecting the window is sometimes I need to be able to change the target of the link to a blank window. That is where I'm running into the issue. The link is being created without issue, and added to the page and the click event is being fired. But when the target is _blank it is actually opening in a popup window instead of a new tab within the browser. The popup window is the same as a window.open().

There are other static links on my page with target=_blank and they open in a new tab without issue. Just the link that is being created behind the scenes and triggered with javascript open this way. Any ideas?

Pure Javascript Approach:

string js = @"var link = document.createElement('a');
                            link.href='{0}';
                            link.target='{1}';
                            document.body.appendChild(link);
                            link.click();
                            link.remove();";
string link = String.Format(js, url, target);

JQuery Approach:

string js = @"$('document').ready(function(){{
                                    $('<a id=""tmpLink"" href=""{0}"" target=""{1}""></a>').appendTo('body');
                                    $('a#tmpLink').trigger('click');                                    
                              }});";
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
jay
  • 434
  • 1
  • 5
  • 25
  • 1
    Did you check your links in e.g. firebug? Are they anyhow different from static ones? Afair e.g. here http://stackoverflow.com/questions/1834559/target-blank-to-show-in-new-window-not-new-tab-possible it is browser dependant thing whether or not something is opened in tab or window – Michael Brennt Jul 10 '14 at 13:49
  • 1
    Triggering click will only make the event fire, and any JavaScript event listeners will respond to that event. Triggering click will not make the browser *actually* click the link. Therefore it won't navigate to that link. – Daniel Apt Jul 10 '14 at 14:00
  • I understand there isn't a way to force a new tab and that it is up to the browser to handle that, I took out the link.remove(); and left the link on the page. When I manually click the link it opens in a new tab. – jay Jul 10 '14 at 14:02
  • Thanks Daniel, that's what I was expecting to find out. – jay Jul 10 '14 at 14:03

0 Answers0