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');
}});";