Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

link in alert boxes javascript

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

like image 741
user2101459 Avatar asked Jun 06 '13 22:06

user2101459


People also ask

Can you put a link in a JavaScript alert?

Displaying hyperlinks in an alert box in JavaScript is not possible. To show links, use a custom alert box. With a dialog alert widget, you can achieve the following: For more on this, refer to the source code for Dialog Alert Widget.

Can we customize alert box in JavaScript?

The standard alert box in JavaScript does not provide the option to apply CSS. To style your alert box, you need to create a custom one first. The custom alert box will be created using jQuery and styles will be applied to CSS.

How do I add inputs to my alert box?

You can't put anything in an alert box. As the name indicates, it's an alert. You might be looking for a prompt which has an input text field, or confirm to get a true / false depending on user selection.

What is an alert box in JavaScript?

Alert Box A JavaScript alert box is used when you need some information to reach the user. When the alert box shows up, the user will need to press the OK button to resume activity. It interrupts the user's activity, so you need to be careful when using it.

How to show hyperlinks in JavaScript alert?

How to show hyperlinks in JavaScript alert? How to show hyperlinks in JavaScript alert? Displaying hyperlinks in an alert box in JavaScript is not possible. To show links, use a custom alert box.

How do I add a button to a custom alert box?

Append the button text to the button element. Append the button element to the alert box. Assign the button element a unique class name. Attach an event listener to the button. The event listener should close the custom alert box.

What is the use of window alert box?

An alert box is often used if you want to make sure information comes through to the user. When an alert box pops up, the user will have to click "OK" to proceed. The window.alert () method can be written without the window prefix.


4 Answers

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';
};
like image 155
warlord Avatar answered Oct 20 '22 12:10

warlord


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

like image 20
Naïm Baki Avatar answered Oct 20 '22 12:10

Naïm Baki


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();
};

👍

like image 5
KRISHNA Avatar answered Oct 20 '22 10:10

KRISHNA


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

like image 4
Antoine Avatar answered Oct 20 '22 11:10

Antoine