JavaScript中警告框中的链接

43

我有一个简单的问题。我有以下代码:

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

变量redirURL是一个实际可用的URL。我希望它能够被“点击”。

谢谢您提前!


1
可能是Can I incorporate a link in a message box in javascript?的重复问题。 - Felix Kling
8个回答

43

使用 window.confirm 而不是 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';
};

4
你怎样才能使它在新窗口中打开? - nzaleski

13

在alert函数中,你只能显示文本。如果你想放置一个URL,可以使用jQuery的对话框功能。以下是一些示例代码:http://jqueryui.com/dialog/#default


1
有没有可能不用查询来做到这一点?我更感兴趣的是纯警报。 - Almas
1
这不是jQuery对话框 - 这是jQueryUI对话框。 - ccpizza

5

如果您想在警示框中打开链接,并在新窗口中打开,请使用以下代码:

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

请注意,大多数浏览器将把这些链接视为弹出窗口。

还有一种替代方法,两者的效果相同,请见下文:

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


4

2

在标准的alert()框中,您无法放置可点击的URL。相反,您可以使用“lightbox”,这是一个HTML弹出窗口——有很多这样的工具可用,您应该选择一个与您的网站/应用程序的其余部分相匹配的工具。


2
这在我知道的任何“标准”Web浏览器中都不可能。

我建议使用更强大的方法,例如jQuery UI的对话框


1

你正在使用的window.alert无法实现此功能。相反,你可以尝试使用对话框插件,例如BootstrapjQuery UI Dialog的模态插件。你的超链接是一个html,而警告框是浏览器通过javascript生成的非html组件。

警告对话框应该用于不需要用户做出响应的消息,除了确认消息之外。

参考资料


0

这是一个使用Jquery对话框的方法

<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="https://dev59.com/NWQn5IYBdhLWcg3wRVRs";
    return false;  },
                Cancel: function() {
                $( this ).dialog( "close" );
                }
            },
            close: {}
        });


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


                    });
</script>
  <body>

<div id="wnd_Addparam" title="Information" ></div>
</body>
</html>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接