195

I'm generating a JavaScript alert with following code in C# .NET page:

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?

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
subash
  • 4,050
  • 14
  • 51
  • 78
  • 2
    possible duplicate of [Change Title of Javascript Alert](http://stackoverflow.com/questions/4172022/change-title-of-javascript-alert) – Sjoerd Jan 25 '13 at 14:47
  • 2
    Do you know, that alerts can be very easily disabled by the user? You should NOT rely on those! – toesslab Aug 29 '14 at 05:24
  • I think the question was for JavaScript which is compatible with all platforms and browsers and not for VBScript which is a strictly IE and Windows thing so its usage is limited to Windows only – UserTursty Dec 31 '12 at 20:08

10 Answers10

307

No, you can't.

It's a security/anti-phishing feature.

user229044
  • 232,980
  • 40
  • 330
  • 338
Pierreten
  • 9,917
  • 6
  • 37
  • 45
  • 29
    While this is an answer and is IN FACT true, I have determined that you can make your own version of alert and it will do what you want. A simple modal and override the alert function call. – Fallenreaper Jan 10 '13 at 17:19
  • 6
    How is this a security/anti-phishing feature? I'm curious, not arguing. – Tim Jan 20 '15 at 19:27
  • 2
    @Tim Presumably, it would be easier to simulate another website if your alerts didn't tell the user what URL they were originating from; disallowing this customization forces JavaScript to tell the user it isn't a legitimate message. – Hydrothermal Jan 28 '15 at 00:57
  • @Hydrothermal Just out of curiosity, can't the alert behavior be simulated by CSS now and if so, is it still much of a threat to allow you to change the modal alert titles? – Josh Apr 29 '15 at 20:43
  • 9
    @Josh You could get close with a lot of hard work, but native browser alerts have behavior that can't be simulated, such as freezing the rest of the browser, floating on top of the browser UI, and functioning as a separate window that can be dragged off of the browser screen. It's very difficult to create a convincing replica, especially since each browser's alert looks different. – Hydrothermal Apr 29 '15 at 20:48
  • I'm with Tim. How is it a security feature if it says "This page says" no matter what link/URL it's coming from? – KWallace Oct 23 '21 at 00:32
68

No, it is not possible. You can use a custom javascript alert box.

Found a nice one using jQuery

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

rahul
  • 184,426
  • 49
  • 232
  • 263
  • 4
    Thank u ,but i am not interested in using custom alert box – subash Dec 15 '09 at 05:17
  • 22
    The reason you can't change the title, by the way, is to prevent malicious web sites from tricking the user into thinking the alert is from their OS or something else. – benzado Dec 15 '09 at 05:43
  • 1
    NOTE: Another user has attempted to edit this answer to indicate that the link supplied led to a site where malware was detected. –  Feb 28 '12 at 13:27
  • No offence but those alerts look very ugly. SweetAlert is much prettier than this. – shashwat Aug 16 '17 at 07:27
  • The link is dead. – Adam_G Nov 04 '21 at 23:19
37

You can do this in 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.)

Chris Fulstow
  • 41,170
  • 10
  • 86
  • 110
  • @Chris Fulstow what can be a solution to [this question](http://stackoverflow.com/questions/9347654/combo-box-is-getting-vanished-when-an-alert-is-coming) – Tom Feb 19 '12 at 08:53
  • 1
    I love vbscript, i wish that vbscript would be standardized along with javascript. that way i can script without the confusion of case sensitivity, and all the popular browers would obey my commands MWHAHAHA – Ronnie Matthews Jun 07 '16 at 08:07
  • 3
    Wow a comment has more votes than the answer... for seconding part of the answer – Marvin Thobejane Aug 12 '16 at 08:39
  • hoho if VBScript were still here – Dee Dec 08 '22 at 15:50
21

I Found this Sweetalert for customize header box 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");
});
Abed Putra
  • 1,153
  • 2
  • 17
  • 37
12

Override the javascript window.alert() function.

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).

Tested working in chrome and webkit, not sure of others.

Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
Derk Jochems
  • 129
  • 1
  • 2
5

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.

If you use the custom script that cxfx provided: (place it in your apsx file)

<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!

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • 2
    Works for me with IE, but not Firefox. Thanks just the same, quick and dirty but might prove useful. – Darth Continent Oct 22 '10 at 13:57
  • 1
    @ Darth. Yo I proly should have mentioned that. Its using VBScript. you have have to google how to get VBScript to run in firefox. – kralco626 Oct 22 '10 at 14:34
  • 2
    @ Dath - PS I don't know if there is a way to possibly run VBScript on firefox. I would create another topic called something like how to run this vb script in firefox and post the code I gave you above and see if anyone can come up with anything. – kralco626 Oct 22 '10 at 14:38
  • 2
    @Darth - Although I would use Rahul's solution. Just use some JQuery it's really easy! – kralco626 Oct 22 '10 at 14:40
  • Thanks, I tried out a couple of JQuery-based alert boxes and they're great. – Darth Continent Oct 27 '10 at 15:22
  • 3
    Two downvotes, one upvote... but no comments to say why I'm being downvoted... I think this is a valid solution... at least in IE... :/ – kralco626 Mar 26 '12 at 10:49
  • @kralco626 you should not bother with downvotes. i have learned a lot from SO, practically all js and php when people were screaming at me about spelling and what not. ignoring them is what really helped me. – webs Nov 02 '21 at 07:45
4

There's quite a nice 'hack' here - https://stackoverflow.com/a/14565029 where 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.

Community
  • 1
  • 1
extraLRG
  • 49
  • 2
4

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>
webzy
  • 338
  • 3
  • 12
1

Yes you can change it. if you call VBscript function within Javascript.

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>
Saeed
  • 21
  • 1
0

I had a similar issue when I wanted to change the box title and button title of the default confirm box. I have gone for the Jquery Ui dialog plugin http://jqueryui.com/dialog/#modal-confirmation

When I had the following:

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

I have changed it to:

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');
}

Can be seen working here https://jsfiddle.net/5aua4wss/2/

Hope that helps.

justMe
  • 2,200
  • 16
  • 20