如何编辑 JavaScript 警告框标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1905289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to edit a JavaScript alert box title?
提问by subash
I'm generating a JavaScript alert with following code in C# .NET page:
我在 C# .NET 页面中使用以下代码生成 JavaScript 警报:
Response.Write("<script language=JavaScript> alert('Hi select a valid date'); </script>");
It displays an alert box with the heading title as "Message from webpage".
它显示一个警告框,标题为“来自网页的消息”。
Is it possible to modify the title?
标题可以修改吗?
回答by Pierreten
No, you can't.
不,你不能。
It's a security/anti-phishing feature.
这是一个安全/反网络钓鱼功能。
回答by rahul
No, it is not possible. You can use a custom javascript alert box.
不,这是不可能的。您可以使用自定义 javascript 警报框。
Found a nice one using jQuery
使用 jQuery 找到了一个不错的
jQuery Alert Dialogs (Alert, Confirm, & Prompt Replacements)
回答by Chris Fulstow
You can do this in IE:
您可以在 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>
(Although, I really wish you couldn't.)
(虽然,我真的希望你不能。)
回答by Derk Jochems
Override the javascript window.alert() function.
覆盖 javascript window.alert() 函数。
window.alert = function(title, message){
var myElementToShow = document.getElementById("someElementId");
myElementToShow.innerHTML = title + "</br>" + message;
}
With this you can create your own alert()function. Create a new 'cool' looking dialog (from some div elements).
有了这个,您可以创建自己的alert()功能。创建一个新的“很酷”的对话框(来自一些 div 元素)。
Tested working in chrome and webkit, not sure of others.
测试在 chrome 和 webkit 中工作,不确定其他人。
回答by Abed Putra
I Found this Sweetalertfor customize header box javascript.
我发现这个Sweetalert用于自定义标题框 javascript。
For example
例如
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");
});
回答by kralco626
To answer the questions in terms of how you asked it.
根据您的提问方式回答问题。
This is actually REALLY easy (in Internet Explorer, at least), i did it in like 17.5 seconds.
这实际上非常简单(至少在 Internet Explorer 中),我在 17.5 秒内完成。
If you use the custom script that cxfx provided: (place it in your apsx file)
如果您使用 cxfx 提供的自定义脚本:(将其放在您的 apsx 文件中)
<script language="VBScript">
Sub myAlert(title, content)
MsgBox content, 0, title
End Sub
</script>
You can then call it just like you called the regular alert. Just modify your code to the following.
然后,您可以像调用常规警报一样调用它。只需将您的代码修改为以下内容。
Response.Write("<script language=JavaScript> myAlert('Message Header Here','Hi select a valid date'); </script>");
Hope that helps you, or someone else!
希望对您或其他人有所帮助!
回答by extraLRG
There's quite a nice 'hack' here - https://stackoverflow.com/a/14565029where you use an iframe with an empty src to generate the alert / confirm message - it doesn't work on Android (for security's sake) - but may suit your scenario.
这里有一个很好的“黑客” - https://stackoverflow.com/a/14565029,您使用带有空 src 的 iframe 来生成警报/确认消息 - 它在 Android 上不起作用(为了安全起见) -但可能适合您的情况。
回答by webzy
You can do a little adjustment to leave a blank line at the top.
您可以稍作调整,在顶部留一个空行。
Like this.
像这样。
<script type="text/javascript" >
alert("USER NOTICE " +"\n"
+"\n"
+"New users are not allowed to work " +"\n"
+"with that feature.");
</script>
回答by Shafiqul Islam
When you start up or just join a project based on webapplications, the design of interface is maybe good. Otherwise this should be changed. In order to Web 2.0 applications you will work with dynamic contents, many effects and other stuff. All these things are fine, but no one thought about to style up the JavaScript alert and confirm boxes. Here is the they way,.. completely dynamic, JS and CSS driven Create simple html file
当你刚开始或刚加入一个基于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>
Then create simple js file name jsConfirmStyle.js. Here is simple js code
然后创建简单的 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()'>"+
" "+
"<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();
}
}
回答by Saeed
Yes you can change it. if you call VBscript function within Javascript.
是的,你可以改变它。如果您在 Javascript 中调用 VBscript 函数。
Here is simple example
这是一个简单的例子
<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>

