How to edit a JavaScript alert box title?

JavascriptAlert

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

No, you can't.

It's a security/anti-phishing feature.

Solution 2 - Javascript

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)

Solution 3 - Javascript

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

Solution 4 - Javascript

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

Solution 5 - Javascript

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.

Solution 6 - Javascript

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!

Solution 7 - Javascript

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.

Solution 8 - Javascript

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>

Solution 9 - Javascript

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>

Solution 10 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsubashView Question on Stackoverflow
Solution 1 - JavascriptPierretenView Answer on Stackoverflow
Solution 2 - JavascriptrahulView Answer on Stackoverflow
Solution 3 - JavascriptChris FulstowView Answer on Stackoverflow
Solution 4 - JavascriptAbed PutraView Answer on Stackoverflow
Solution 5 - JavascriptDerk JochemsView Answer on Stackoverflow
Solution 6 - Javascriptkralco626View Answer on Stackoverflow
Solution 7 - JavascriptextraLRGView Answer on Stackoverflow
Solution 8 - JavascriptwebzyView Answer on Stackoverflow
Solution 9 - JavascriptSaeedView Answer on Stackoverflow
Solution 10 - JavascriptjustMeView Answer on Stackoverflow