2

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.

Community
  • 1
  • 1
Richard de Wit
  • 7,102
  • 7
  • 44
  • 54
  • 1
    you might want to append it before clicking it ? O.o – Patsy Issa Jul 31 '12 at 08:20
  • No I don't want it to be in the HTML source – Richard de Wit Jul 31 '12 at 08:22
  • `window.open("dostuff.php")`? – Andreas Jul 31 '12 at 08:23
  • 2
    You are setting the "src" attribute, which does not exist in a link. You should set the "href" one – davids Jul 31 '12 at 08:25
  • @davids omg.. Im stupid, thanks! – Richard de Wit Jul 31 '12 at 08:27
  • Anyway, 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? – davids Jul 31 '12 at 08:29
  • @Andreas `window.open` tends to show a popupwarning in IE, so I prefer not to use that – Richard de Wit Jul 31 '12 at 08:30
  • Try 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. – Tgr Jul 31 '12 at 08:55
  • Take a look at this answer http://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 – davids Jul 31 '12 at 08:56
  • What you want to do looks very fishy to me... – Christoph Jul 31 '12 at 09:06
  • It'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](http://stackoverflow.com/a/11736910/1062281) – Richard de Wit Jul 31 '12 at 09:27
  • @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. – Christoph Jul 31 '12 at 13:56
  • @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 Jul 31 '12 at 13:58

3 Answers3

4

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:

  1. Create a form
  2. Give it attributes
  3. Append it to the DOM so it can be submitted
  4. Submit it
  5. 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).

Community
  • 1
  • 1
Richard de Wit
  • 7,102
  • 7
  • 44
  • 54
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();
Richard de Wit
  • 7,102
  • 7
  • 44
  • 54
mwld
  • 225
  • 2
  • 8
  • Seriously? I forgot the `#`... The click function works now, but the default action (i.e. opening the link) isn't happening – Richard de Wit Jul 31 '12 at 08:59
1
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?

helle
  • 11,183
  • 9
  • 56
  • 83