2009-12-15 162 views
146

我生成一个JavaScript警告与下面的C#.NET页面代码:如何编辑JavaScript警告框标题?

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

它显示标题标题“从网页信息”的提示框。

是否可以修改标题?

+1

[JavaScript警告的更改标题]的可能重复(http://stackoverflow.com/questions/4172022/change-title-of-javascript-alert) – Sjoerd 2013-01-25 14:47:24

+2

你知道,警报可以很容易地被用户禁用?你不应该依赖这些! – 2014-08-29 05:24:38

+0

我认为问题是针对与所有平台和浏览器兼容的JavaScript,而不是针对VBScript的,这是严格的IE和Windows操作,所以它的使用仅限于Windows。 – UserTursty 2012-12-31 20:08:52

回答

227

叹息...不,你不能。

它的安全/反钓鱼功能。

+16

尽管这是一个答案,并且是IN事实是,我已经确定你可以制作你自己的警报版本,它会做你想做的。一个简单的模式并覆盖警报函数调用。 – Fallenreaper 2013-01-10 17:19:30

+58

@Fallenreaper停止打破互联网。 – 2014-01-27 05:33:16

+1

这是一个安全/反钓鱼功能?我很好奇,没有争辩。 – Tim 2015-01-20 19:27:47

64

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

发现使用jQuery

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

+3

谢谢你,但我对使用自定义警报框不感兴趣 – subash 2009-12-15 05:17:44

+18

顺便说一下,无法更改标题的原因是为了防止恶意网站诱骗用户认为警报来自操作系统或其他内容。 – benzado 2009-12-15 05:43:20

+1

注意:另一个用户试图编辑此答案以表明提供的链接导致检测到恶意软件的站点。 – 2012-02-28 13:27:40

30

一个很好的一个可以在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> 

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

+52

我们都希望相同 – rahul 2009-12-15 05:28:34

+0

@Chris Fulstow什么可以解决[这个问题](http://stackoverflow.com/questions/9347654/combo-box-is-getting-vanished-when-an-alert-is -coming) – Tom 2012-02-19 08:53:18

+1

我喜欢vbscript,我希望vbscript会与javascript一起标准化。这样我可以脚本没有区分大小写的混淆,并且所有流行的浏览器都会服从我的命令MWHAHAHA – 2016-06-07 08:07:09

4

按照您问的方式回答问题。

这实际上很简单(至少在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>"); 

希望能帮到你,或者别人!

+2

适用于IE,但不适用于Firefox。感谢同样的,快速和肮脏的,但可能会证明是有用的。 – 2010-10-22 13:57:25

+1

@达思。哟,我应该提到这一点。它使用VBScript。你必须谷歌如何让VBScript在Firefox中运行。 – kralco626 2010-10-22 14:34:10

+2

@ Dath - PS我不知道是否有方法可能在Firefox上运行VBScript。我会创建另一个话题,比如如何在firefox中运行这个VB脚本,并发布我上面给出的代码,看看是否有人可以提出任何问题。 – kralco626 2010-10-22 14:38:01

1

是的,你可以改变它。如果你在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> 
+2

嗨@Saeed,它的工作在IE浏览器,但它不会在Mozilla和铬... .... – 2012-08-24 07:30:37

+1

@ManikandanSethuraju它不会在FF和GC工作,因为他们不支持VBScript。 – atsurti 2013-10-08 00:52:53

10

覆盖的JavaScript window.alert()函数。

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

使用此功能,您可以创建自己的alert()功能。创建一个新的'酷'看对话框(从一些div元素)。

在chrome和webkit中测试过,不知道其他人。

+0

什么是'someElementId'?我的意思是哪个标签? – 2013-04-16 08:53:00

+1

'someElementId'就是你设置HTML元素的id。 – 2013-08-25 16:14:20

4

这里有一个相当不错的'黑客' - https://stackoverflow.com/a/14565029您使用带有空src的iframe生成警报/确认消息 - 它不适用于Android(出于安全考虑) - 但可能适合您的场景。

+1

优秀的,非常有用的... – 2013-05-13 18:35:49

+1

不仅仅是Android的工作。 – 2014-08-23 13:31:10

1

当你启动或者只是加入一个基于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

+1

你可以做一个demu plz – 2015-09-12 06:29:17

0

你可以做一个小的调整留在顶部的空行。

像这样。

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

我发现这个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"); 
});