Anoop Pranav Anoop Pranav - 3 months ago 13
jQuery Question

open new tab only after click popup ok jquery

How to create a script for open a tag href url in new window or tab only after clicking ok button in popup. there are multiple a tags with different urls.

$(document).ready(function() {

$("[target=_blank]").click(function() {
$(".popup").toggle();
});
$(".cancel").click(function() {
$(".popup").hide();
});
});




http://jsfiddle.net/xdfoydy8/4/

Answer

You can do it in following way:

http://jsfiddle.net/xdfoydy8/5/

$(document).ready(function() {

  $("[target=_blank]").click(function(e) {
    e.preventDefault();
    $(".popup").show();
    $("a.ok").data("hrefData", $(this).attr("href"));
  });
  $(".cancel").click(function() {
    $(".popup").hide();
  });

  $("a.ok").click(function() {
    var href = $(this).data("hrefData");
    if ($.trim(href).length > 0) {
      window.open(href, "_blank");
    }
  });
});

We are setting the clicked href in ok link's $.data() and retrieving it on click of ok click and taking user to clicked href

Comments