202

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?

3
  • 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
    Commented Dec 31, 2012 at 20:08
  • 2
    possible duplicate of Change Title of Javascript Alert
    – Sjoerd
    Commented Jan 25, 2013 at 14:47
  • 2
    Do you know, that alerts can be very easily disabled by the user? You should NOT rely on those! Commented Aug 29, 2014 at 5:24

11 Answers 11

318

No, you can't.

It's a security/anti-phishing feature.

6
  • 31
    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. Commented Jan 10, 2013 at 17:19
  • 8
    How is this a security/anti-phishing feature? I'm curious, not arguing.
    – Tim
    Commented Jan 20, 2015 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. Commented Jan 28, 2015 at 0: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
    Commented Apr 29, 2015 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. Commented Apr 29, 2015 at 20:48
67

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)

5
  • 5
    Thank u ,but i am not interested in using custom alert box
    – subash
    Commented Dec 15, 2009 at 5: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
    Commented Dec 15, 2009 at 5: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.
    – user359040
    Commented Feb 28, 2012 at 13:27
  • No offence but those alerts look very ugly. SweetAlert is much prettier than this.
    – shashwat
    Commented Aug 16, 2017 at 7:27
  • 1
    The link is dead.
    – Adam_G
    Commented Nov 4, 2021 at 23:19
36

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

3
  • 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 Commented Jun 7, 2016 at 8:07
  • 3
    Wow a comment has more votes than the answer... for seconding part of the answer Commented Aug 12, 2016 at 8:39
  • hoho if VBScript were still here
    – Dan D
    Commented Dec 8, 2022 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");
});
1
  • 1
    Yeah. SweeAlert is even simpler that your example if you want. I just made a <script link to their library and replaced the word "alert" in my code and there it was! Thank you. Sweet recommendation!
    – JustJohn
    Commented Dec 8, 2019 at 7:00
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.

3
  • 1
    what is someElementId? which tag I mean? Commented Apr 16, 2013 at 8:53
  • 1
    someElementId is whatever id you've set your HTML element as.
    – James P.
    Commented Aug 25, 2013 at 16:14
  • Appears to no longer work with modern browsers
    – yoel halb
    Commented Jul 2, 2021 at 21:18
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!

7
  • 2
    Works for me with IE, but not Firefox. Thanks just the same, quick and dirty but might prove useful. Commented Oct 22, 2010 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
    Commented Oct 22, 2010 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
    Commented Oct 22, 2010 at 14:38
  • 2
    @Darth - Although I would use Rahul's solution. Just use some JQuery it's really easy!
    – kralco626
    Commented Oct 22, 2010 at 14:40
  • 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
    Commented Mar 26, 2012 at 10:49
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.

0
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>
1
  • yeah. if you cannot beat them. join them.
    – webs
    Commented Nov 2, 2021 at 7:46
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>
1
  • 1
    @ManikandanSethuraju it will not work on FF and GC as they don't support VBScript.
    – atsurti
    Commented Oct 8, 2013 at 0:52
1

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.

0

You can use a native HTML dialog modal instead. Can be fully styled with CSS, minimal code, accessible, just works in modern browsers.

// JS to show the modal:
myModal.showModal();
<dialog id="myModal">
     <p>Hi select a valid date</p>
     <button onclick="myModal.close()">OK</button>     
</dialog>

Even a confirm/cancel dialog:

<dialog id="myModal">
     <p>Are you sure you want to delete all?</p>
     <button onclick="myModal.close()">Cancel</button>
     <button onclick="myModal.close();deleteAll();">Delete</button>
</dialog>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.