42

I have a simple question. I have the following code:

alert("Are you sure you want to add: \n" + redirURL + "?");

the variable redirURL is an actual working url. I would like it to be 'clickable'

thank you in advance

user2101459
  • 579
  • 2
  • 8
  • 19
  • 1
    possible duplicate of [Can I incorporate a link in a message box in javascript?](http://stackoverflow.com/questions/966848/can-i-incorporate-a-link-in-a-message-box-in-javascript) – Felix Kling Jun 06 '13 at 22:27

8 Answers8

41

use window.confirm instead of alert

if (window.confirm('If you click "ok" you would be redirected . Cancel will load this website ')) 
{
window.location.href='https://www.google.com/chrome/browser/index.html';
};
Machavity
  • 30,841
  • 27
  • 92
  • 100
warlord
  • 427
  • 1
  • 4
  • 2
13

You can only display text in the alert function. If you want to put an url, you can do it by using jquery's dialog function. Here are some code examples: http://jqueryui.com/dialog/#default

Amit Kumar Pawar
  • 3,252
  • 1
  • 20
  • 27
Naïm Baki
  • 197
  • 6
5

If you want to open the Link on alert and that too in new window, Use the code below:

if (window.confirm('Ok to Confirm, Cancel to Stay here'))
   {
   window.open('http://www.google.com', '_blank');
   };

Note that most browsers will treat these links as popup.

There is an alternative too, Both works same, find it below:

//write this above first
let a=document.createElement('a');
a.target='_blank';
a.href='https://www.google.com';

//then use this code for alert
if (window.confirm('Ok to Confirm, Cancel to Stay here'))
{
a.click();
};

KRISHNA
  • 189
  • 3
  • 8
4

That's not possible to put clickable links in alert windows. The closest thing you could do is using a modal window, like this: http://twitter.github.io/bootstrap/javascript.html#modals

Antoine
  • 2,785
  • 1
  • 16
  • 23
2

You can't put clickable URLs in a standard alert() box. Instead you could use a "lightbox", which is an HTML popup - there are any number of them available, and you should choose one that fits well with the rest of your site/applicaiton.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
2

It's not possible in any "standard" web browser that I'm aware of.

I'd suggest using a more robust approach like jQuery UI's dialog.

jterry
  • 6,209
  • 2
  • 30
  • 36
1

It is not possible with window.alert that you are using. Instead you can try using dialog plugins like modal plugin from bootstrap or jquery ui dialog. Your hyperlink is an html where as alert box is non html component of the browser generated by browser's javascript.

The alert dialog should be used for messages which do not require any response on the part of the user, other than the acknowledgement of the message.

Reference

PSL
  • 123,204
  • 21
  • 253
  • 243
0

This is a method with Jquery's Dialog

<html>
  <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <style></style>
  </head>
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script src='template/js/jquery.textarea-expander.js'></script>
 <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript">
// <---- VENTAÑAS DE PARAMETERES---->
$(document).ready(function() { 
var regex,v,l,c,b,i,contapara=3;




$( "#wnd_Addparam" ).dialog({
            autoOpen: false,
            height: 'auto',
            width: 350,
            modal: true,
            resizable:false,
            buttons: {
                "Link": function() {
                   location.href="http://stackoverflow.com/questions/16973240/link-in-alert-boxes-javascript";
    return false;  },
                Cancel: function() {
                $( this ).dialog( "close" );
                }
            },
            close: {}
        });


                $( "#wnd_Addparam" ).dialog( "open" );


                    });
</script>
  <body>

<div id="wnd_Addparam" title="Information" ></div>
</body>
</html>
Mirko Cianfarani
  • 2,023
  • 1
  • 23
  • 39