2009-12-15 55 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"); 
});