Opening new window/tab without using `window.open` or `window.location.href`

15,406

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:

  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).

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?

Share:
15,406

Related videos on Youtube

Richard de Wit
Author by

Richard de Wit

| Full ≡ Web Developer ðŸ‘ĻðŸŧ‍ðŸ’ŧ | Linux enthousiast 🐧 | Dad ðŸ‘Ļ‍ðŸ‘Đ‍ðŸ‘Ķ‍ðŸ‘Ķ | Gamer âŒĻïļðŸ–ąïļ Codes in Python 🐍 Ruby 💎 PHP 🐘 Bash 👊ðŸŧ JavaScript ðŸĪŠ

Updated on June 04, 2022

Comments

  • Richard de Wit
    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
      Patsy Issa almost 12 years
      you might want to append it before clicking it ? O.o
    • Richard de Wit
      Richard de Wit almost 12 years
      No I don't want it to be in the HTML source
    • Andreas
      Andreas almost 12 years
      window.open("dostuff.php")?
    • davids
      davids almost 12 years
      You are setting the "src" attribute, which does not exist in a link. You should set the "href" one
    • Richard de Wit
      Richard de Wit almost 12 years
      @davids omg.. Im stupid, thanks!
    • davids
      davids almost 12 years
      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?
    • Richard de Wit
      Richard de Wit almost 12 years
      @Andreas window.open tends to show a popupwarning in IE, so I prefer not to use that
    • Tgr
      Tgr almost 12 years
      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.
    • davids
      davids almost 12 years
      Take 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
      Christoph almost 12 years
      What you want to do looks very fishy to me...
    • Richard de Wit
      Richard de Wit almost 12 years
      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
    • Christoph
      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
      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
    Richard de Wit almost 12 years
    Seriously? I forgot the #... The click function works now, but the default action (i.e. opening the link) isn't happening