1

I'm trying to have an onlick alert, then redirect to a new window

This is what I have so far

<button type="button" class="button" onclick="pop()">Renew Subscription</button>
</div>

<script>
function pop() {
alert("40% Discount has been applied");
window.location.href="http://LINKHERE.COM", '_blank';
}
</script>

Right now redirects in the same window but prompts a "leave site? Changes you made may not be saved."

3 Answers3

0

This is working code

Execute it outside stackoverflow, its not working here I don't know why?

function pop() {
alert("40% Discount has been applied");
window.open("https://www.wikipedia.org/","_blank");
}
<button type="button" class="button" onclick="pop()">Renew Subscription</button>
siddesh
  • 41
  • 3
0

Try window.open():

 <!DOCTYPE html>
    <html>
    <body>
    <button type="button" class="button" onclick="pop()">Renew Subscription</button>
    <script>
    function pop() {
    alert("40% Discount has been applied");
    var win = window.open("http://LINKHERE.COM", '_blank');
      win.focus();
    }
    </script>
    </body>
    </html>
sa_n__u
  • 336
  • 1
  • 9
0

You can use third party libraries like SweetAlert2 to easily achieve this.

function pop() {
  Swal.fire({
    title: 'Success!!',
    text: "40% Discount has been applied",
    type: 'success',
    confirmButtonColor: '#3085d6',
    confirmButtonText: 'Great!'
  }).then((result) => {
    if (result.value) {
      window.open('https://www.google.com', '_blank');
    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>

<button type="button" class="button" onclick="pop()">Renew Subscription</button>