Opening new window/tab without using `window.open` or `window.location.href`
Solution 1
I have the answer. Apparently jQuery doesn't support the default behavior of links clicked programmatically
Creating and submitting a form works really well though (tested in Chrome 26
, FF 20
and IE 8
):
var form = $("<form></form>");
form.attr(
{
id : "newform",
action : "https://google.nl",
method : "GET",
target : "_blank" // Open in new window/tab
});
$("body").append(form);
$("#newform").submit();
$("#newform").remove();
What it does:
- Create a form
- Give it attributes
- Append it to the DOM so it can be submitted
- Submit it
- Remove the form from the DOM
Now you have a new tab/window loading "https://google.nl" (or any URL you want, just replace it). Unfortunately when you try to open more than one window at once this way, you get an Popup blocked messagebar when trying to open the second one (the first one is still opened).
Solution 2
Regarding your updated script: If you get the selector right ($("#linky")
) it works.
var link = $("<a id='linky'>Hello</a>");
link.attr("href", "/dostuff.php");
link.attr("target", "_blank");
$("body").append(link);
$("#linky").on("click", function() {alert("hai");});
$("#linky").click();
$("#linky").remove();
Solution 3
var link = $("<a id='linky'></a>");
link.attr("href", "/dostuff.php");
link.attr("target", "_blank");
$("body").append(link);
$("linky").live("click", function() {alert("hai"); $(this).remove()});
$("linky").click();
//$("linky").remove();
So you actually react on the click event. And YES! The question is why would you want to do this?
Related videos on Youtube
![Richard de Wit](https://i.stack.imgur.com/xQZA7.png?s=256&g=1)
Richard de Wit
| Full ⥠Web Developer ðĻðŧâðŧ | Linux enthousiast ð§ | Dad ðĻâðĐâðĶâðĶ | Gamer âĻïļðąïļ Codes in Python ð Ruby ð PHP ð Bash ððŧ JavaScript ðĪŠ
Updated on June 04, 2022Comments
-
Richard de Wit about 2 years
I want to generate a link that is clicked right after creating, but nothing happens
Code:
var link = $("<a></a>"); link.attr("href", "/dostuff.php"); link.attr("target", "_blank"); link.click();
The attributes are set correctly:
var link = $("<a></a>"); link.attr("href", "/dostuff.php"); link.attr("target", "_blank"); var linkcheck = link.wrap('<p>').parent().html(); console.log(linkcheck);
This returns:
<a href="/dostuff.php" target="_blank"></a>
No errors
UPDATE
I tried to append it, bind click to it, click it and remove it
var link = $("<a></a>"); link.attr( { id : "linky", href : "/dostuff.php", target: "_blank" }); $("body").append(link); $("#linky").on("click", function() { console.log("Link clicked"); }); $("#linky").click(); $("#linky").remove();
The click action is being executed, but the default action (open the link) isn't..
UPDATE2
I have found the solution: creating and submitting a
<form>
! See my answer.-
Patsy Issa almost 12 yearsyou might want to append it before clicking it ? O.o
-
Richard de Wit almost 12 yearsNo I don't want it to be in the HTML source
-
Andreas almost 12 years
window.open("dostuff.php")
? -
davids almost 12 yearsYou are setting the "src" attribute, which does not exist in a link. You should set the "href" one
-
Richard de Wit almost 12 years@davids omg.. Im stupid, thanks!
-
davids almost 12 yearsAnyway, I'm trying to open the link the way you are going, and it doesn't work. I would follow @Andreas approach. Why do you create a link to open a new window?
-
Richard de Wit almost 12 years@Andreas
window.open
tends to show a popupwarning in IE, so I prefer not to use that -
Tgr almost 12 yearsTry to add text inside the link. Anyway, click() might or might not trigger the default action (ie. opening the new page) depending on the browser, and modern browsers are pretty good at catching tricks intended for circumventing the popup protection.
-
davids almost 12 yearsTake a look at this answer stackoverflow.com/a/9081639/978515 According to it, you can't avoid using the open method, if you want to open the link in a new window
-
Christoph almost 12 yearsWhat you want to do looks very fishy to me...
-
Richard de Wit almost 12 yearsIt's not fishy at all, I just don't want any browser to interfere with my webapp ;) Sorry to answer my own question, but I have the solution. See my answer
-
Christoph almost 12 years@GeenHenk It is absolutely okay to answer your own question on Stackoverflow! You should accept it too, so that people know, your problem has been solved.
-
Richard de Wit almost 12 years@Christoph Ok thanks. And I'll accept it as soon as the 2 day wait ends...
You can accept your own answer in 2 days
-
-
Richard de Wit almost 12 yearsSeriously? I forgot the
#
... The click function works now, but the default action (i.e. opening the link) isn't happening