【问题标题】:How to edit a JavaScript alert box title?如何编辑 JavaScript 警告框标题?
【发布时间】:2010-12-26 16:36:22
【问题描述】:

我正在 C# .NET 页面中使用以下代码生成 JavaScript 警报:

Response.Write("<script language=JavaScript> alert('Hi select a valid date'); </script>");

它会显示一个警告框,标题为“来自网页的消息”。

可以修改标题吗?

【问题讨论】:

  • 我认为问题在于与所有平台和浏览器兼容的 JavaScript,而不是针对严格 IE 和 Windows 的 VBScript,因此它的使用仅限于 Windows
  • 您知道,用户可以很容易地禁用警报吗?你不应该依赖那些!

标签: javascript alert


【解决方案1】:

不,你不能。

这是一种安全/反网络钓鱼功能。

【讨论】:

  • 虽然这是一个答案并且事实上是正确的,但我已经确定你可以制作自己的警报版本,它会做你想做的事。一个简单的模态并覆盖警报函数调用。
  • 这是一项安全/反网络钓鱼功能如何?我很好奇,不是在争论。
  • @Tim 据推测,如果您的警报没有告诉用户他们来自哪个 URL,那么模拟另一个网站会更容易;禁止这种自定义会强制 JavaScript 告诉用户它不是合法消息。
  • @Hydrothermal 只是出于好奇,现在不能用 CSS 模拟警报行为吗?如果是这样,允许您更改模式警报标题是否仍然存在很大威胁?
  • @Josh 你可以通过大量的努力来接近,但原生浏览器警报具有无法模拟的行为,例如冻结浏览器的其余部分,浮动在浏览器 UI 之上, 并作为一个单独的窗口运行,可以从浏览器屏幕中拖出。创建一个令人信服的副本非常困难,尤其是因为每个浏览器的警报看起来都不同。
【解决方案2】:

不,这是不可能的。您可以使用自定义 javascript 警报框。

用 jQuery 找到一个不错的

jQuery Alert Dialogs (Alert, Confirm, & Prompt Replacements)

【讨论】:

  • 谢谢你,但我对使用自定义警报框不感兴趣
  • 顺便说一句,您不能更改标题的原因是为了防止恶意网站欺骗用户认为警报来自他们的操作系统或其他东西。
  • 注意:另一位用户试图编辑此答案以表明提供的链接指向检测到恶意软件的站点。
  • 无意冒犯,但那些警报看起来很丑陋。 SweetAlert 比这更漂亮。
  • 链接已失效。
【解决方案3】:

您可以在 IE 中执行此操作:

<script language="VBScript">
Sub myAlert(title, content)
      MsgBox content, 0, title
End Sub
</script>

<script type="text/javascript">
myAlert("My custom title", "Some content");
</script>

(虽然,我真的希望你不能。)

【讨论】:

  • @Chris Fulstow 什么可以解决this question
  • 我喜欢 vbscript,我希望 vbscript 将与 javascript 一起标准化。这样我就可以在不区分大小写的情况下编写脚本,并且所有流行的浏览器都会服从我的命令 MWHAHAHA
  • 哇,评论的票数比答案的票数还多……支持部分答案
【解决方案4】:

我发现这个 Sweetalert 用于自定义标题框 javascript。

例如

swal({
  title: "Are you sure?",
  text: "You will not be able to recover this imaginary file!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
},
function(){
  swal("Deleted!", "Your imaginary file has been deleted.", "success");
});

【讨论】:

  • 是的。如果您愿意,SweeAlert 甚至比您的示例更简单。我刚刚在他们的库中创建了一个
【解决方案5】:

重写 javascript window.alert() 函数。

window.alert = function(title, message){
    var myElementToShow = document.getElementById("someElementId");
    myElementToShow.innerHTML = title + "</br>" + message; 
}

有了它,您可以创建自己的alert() 函数。创建一个新的“酷”对话框(来自一些 div 元素)。

在 chrome 和 webkit 中测试过,其他的不确定。

【讨论】:

  • 什么是someElementId?我指的是哪个标签?
  • someElementId 是您将 HTML 元素设置为的任何 ID。
  • 似乎不再适用于现代浏览器
【解决方案6】:

根据您的提问方式回答问题。

这实际上非常简单(至少在 Internet Explorer 中),我在 17.5 秒内完成了。

如果您使用 cxfx 提供的自定义脚本:(将其放在您的 apsx 文件中)

<script language="VBScript">
Sub myAlert(title, content)
MsgBox content, 0, title 
End Sub 
</script>

然后您可以像调用常规警报一样调用它。只需将您的代码修改为以下内容。

Response.Write("<script language=JavaScript> myAlert('Message Header Here','Hi select a valid date'); </script>");

希望对您或其他人有所帮助!

【讨论】:

  • 适用于 IE,但不适用于 Firefox。同样感谢,快速而肮脏,但可能很有用。
  • @达斯。哟我proly应该提到这一点。它使用VBScript。你必须谷歌如何让 VBScript 在 Firefox 中运行。
  • @ Dath - PS 我不知道是否有办法在 Firefox 上运行 VBScript。我将创建另一个主题,称为如何在 firefox 中运行这个 vb 脚本,并发布我在上面给你的代码,看看是否有人能提出任何建议。
  • @Darth - 虽然我会使用 Rahul 的解决方案。使用一些 JQuery 真的很简单!
  • 两张反对票,一张赞成票...但没有 cmets 说明为什么我被反对票...我认为这是一个有效的解决方案...至少在 IE 中...:/
【解决方案7】:

这里有一个很好的“hack”——https://stackoverflow.com/a/14565029,你使用带有空 src 的 iframe 来生成警报/确认消息——它在 Android 上不起作用(为了安全起见)——但可能适合你的场景.

【讨论】:

    【解决方案8】:

    您可以稍作调整,在顶部留一个空行。

    像这样。

            <script type="text/javascript" >
                alert("USER NOTICE "  +"\n"
                +"\n"
                +"New users are not allowed to work " +"\n"
                +"with that feature.");
            </script>
    

    【讨论】:

    • 是的。如果你不能打败他们。加入他们。
    【解决方案9】:

    是的,您可以更改它。如果你在 Javascript 中调用 VBscript 函数。

    这是一个简单的例子

    <script>
    
    function alert_confirm(){
    
          customMsgBox("This is my title","how are you?",64,0,0,0);
    }
    
    </script>
    
    
    <script language="VBScript">
    
    Function customMsgBox(tit,mess,icon,buts,defs,mode)
       butVal = icon + buts + defs + mode
       customMsgBox= MsgBox(mess,butVal,tit)
    End Function
    
    </script>
    
    <html>
    
    <body>
    <a href="javascript:alert_confirm()">Alert</a>
    </body>
    
    </html>
    

    【讨论】:

    • @ManikandanSethuraju 它不适用于 FF 和 GC,因为它们不支持 VBScript。
    【解决方案10】:

    当你开始或刚加入一个基于 web 应用程序的项目时,界面的设计可能是好的。否则这应该改变。为了 Web 2.0 应用程序,您将使用动态内容、许多效果和其他东西。所有这些都很好,但没有人考虑过设计 JavaScript 警报和确认框的样式。 这是他们的方式,.. 完全动态,JS 和 CSS 驱动 创建简单的html文件

    <html>
     <head>
       <title>jsConfirmSyle</title>
       <meta http-equiv="Content-Style-Type" content="text/css" />
       <meta http-equiv="Content-Script-Type" content="text/javascript" />
        <script type="text/javascript" src="jsConfirmStyle.js"></script>
        <script type="text/javascript">
    
          function confirmation() {
           var answer = confirm("Wanna visit google?")
           if (answer){
           window.location = "http://www.google.com/";
           }
         }    
    
        </script>
        <style type="text/css">
         body {
          background-color: white;
          font-family: sans-serif;
          }
        #jsconfirm {
          border-color: #c0c0c0;
          border-width: 2px 4px 4px 2px;
          left: 0;
         margin: 0;
         padding: 0;
         position: absolute;
        top: -1000px;
        z-index: 100;
       }
    
      #jsconfirm table {
       background-color: #fff;
       border: 2px groove #c0c0c0;
       height: 150px;
       width: 300px;
      }
    
       #jsconfirmtitle {
      background-color: #B0B0B0;
      font-weight: bold;
      height: 20px;
      text-align: center;
    }
    
     #jsconfirmbuttons {
    height: 50px;
    text-align: center;
     }
    
    #jsconfirmbuttons input {
    background-color: #E9E9CF;
    color: #000000;
    font-weight: bold;
    width: 125px;
    height: 33px;
    padding-left: 20px;
    }
    
    #jsconfirmleft{
    background-image: url(left.png);
    }
    
    #jsconfirmright{
    background-image: url(right.png);
     }
     < /style>
      </head>
     <body>
    <p><br />
    <a href="#"
    onclick="javascript:showConfirm('Please confirm','Are you really really sure to visit    google?','Yes','http://www.google.com','No','#')">JsConfirmStyled</a></p>
    <p><a href="#" onclick="confirmation()">standard</a></p>
    
    </body>
    </html>
    

    然后创建简单的js文件名jsConfirmStyle.js。这是简单的js代码

    ie5=(document.getElementById&&document.all&&document.styleSheets)?1:0;
    nn6=(document.getElementById&&!document.all)?1:0;
    
     xConfirmStart=800;
     yConfirmStart=100;
    
      if(ie5||nn6) {
      if(ie5) cs=2,th=30;
      else cs=0,th=20;
       document.write(
        "<div id='jsconfirm'>"+
            "<table>"+
                "<tr><td id='jsconfirmtitle'></td></tr>"+
                "<tr><td id='jsconfirmcontent'></td></tr>"+
                "<tr><td id='jsconfirmbuttons'>"+
                    "<input id='jsconfirmleft' type='button' value='' onclick='leftJsConfirm()' onfocus='if(this.blur)this.blur()'>"+
                    "&nbsp;&nbsp;"+
                    "<input id='jsconfirmright' type='button' value='' onclick='rightJsConfirm()' onfocus='if(this.blur)this.blur()'>"+
                "</td></tr>"+
            "</table>"+
        "</div>"
      );
       }
    
     document.write("<div id='jsconfirmfade'></div>");
    
    
     function leftJsConfirm() {
      document.getElementById('jsconfirm').style.top=-1000;
      document.location.href=leftJsConfirmUri;
     }
    function rightJsConfirm() {
    document.getElementById('jsconfirm').style.top=-1000;
    document.location.href=rightJsConfirmUri;
     }
    function confirmAlternative() {
    if(confirm("Scipt requieres a better browser!"))       document.location.href="http://www.mozilla.org";
    }
    
    leftJsConfirmUri = '';
    rightJsConfirmUri = '';
    
      /**
       * Show the message/confirm box
      */
        function       showConfirm(confirmtitle,confirmcontent,confirmlefttext,confirmlefturi,confirmrighttext,con      firmrighturi)  {
    document.getElementById("jsconfirmtitle").innerHTML=confirmtitle;
    document.getElementById("jsconfirmcontent").innerHTML=confirmcontent;
    document.getElementById("jsconfirmleft").value=confirmlefttext;
    document.getElementById("jsconfirmright").value=confirmrighttext;
    leftJsConfirmUri=confirmlefturi;
    rightJsConfirmUri=confirmrighturi;
    xConfirm=xConfirmStart, yConfirm=yConfirmStart;
    if(ie5) {
        document.getElementById("jsconfirm").style.left='25%';
        document.getElementById("jsconfirm").style.top='35%';
    }
    else if(nn6) {
        document.getElementById("jsconfirm").style.top='25%';
        document.getElementById("jsconfirm").style.left='35%';
    }
    else confirmAlternative();
    

    }

    You can download full Source code from here

    【讨论】:

    • 你能做一个demu吗
    【解决方案11】:

    当我想更改默认确认框的框标题和按钮标题时,我遇到了类似的问题。我已经选择了 Jquery Ui 对话框插件http://jqueryui.com/dialog/#modal-confirmation

    当我有以下情况时:

    function testConfirm() {
      if (confirm("Are you sure you want to delete?")) {
        //some stuff
      }
    }
    

    我已经改成:

    function testConfirm() {
    
      var $dialog = $('<div></div>')
        .html("Are you sure you want to delete?")
        .dialog({
          resizable: false,
          title: "Confirm Deletion",
          modal: true,
          buttons: {
            Cancel: function() {
              $(this).dialog("close");
            },
            "Delete": function() {
              //some stuff
              $(this).dialog("close");
            }
          }
        });
    
      $dialog.dialog('open');
    }
    

    可以看到在这里工作https://jsfiddle.net/5aua4wss/2/

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-16
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多