4

I have this code and I am trying to run it on a .NET platform but it is not working. Does anyone have any idea what is wrong with my code? Thanks. I am using visual studio 2010, and c# programming language.

private void AlertWithConfirmation()
            {
                Response.Write("<script language='javascript'>");
                Response.Write("var x=window.confirm(\"Are you sure you are ok?\")");
                Response.Write("if (x)");
                Response.Write("window.alert(\"Good!\")");
                Response.Write("else");
                Response.Write("window.alert(\"Too bad\")");
                Response.Write("</script>");
            }
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
mikespiteri
  • 901
  • 4
  • 13
  • 25

4 Answers4

3

Your code produces this:

<script language='javascript'>var x=window.confirm("Are you sure you are ok?")if (x)window.alert("Good!")elsewindow.alert("Too bad")</script>

Note the elsewindow identifier that comes from the lack of separator between the commands, which of course does not exist. It will cause an error because the undefined value doesn't have an alert method.

Some improvements:

  • Use the type attribute instead of the deprecated langauge attribute.
  • Use semicolons at the end of statements.
  • Use brackets around code blocks (e.g. following if).
  • Use the return value from confirm directly instead of polluting the global namespace with a variable.
  • Write it as a single string instead of a bunch of strings.

:

private void AlertWithConfirmation() {
  Response.Write(
    "<script type=\"text/javascript\">" +
    "if (window.confirm('Are you sure you are ok?')) {" +
    "window.alert('Good!');" +
    "} else {" +
    "window.alert('Too bad');" +
    "}" + 
    "</script>"
  );
}

Note that if you use this within a regular page, it will write the script tag before the doctype tag, which will cause the browser to go into quirks mode, which will most likely mess up your layout. If you want to add scripts to a regular page you should put a PlaceHolder on the page where you can add it, or use the ClientScriptManager.RegisterStartupScript method.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Make sure the result of the Response.Write looks something like this:

<script type="text/javascript">
    var x=window.confirm('Are you sure you are ok?');
    if (x) {
        window.alert('Good!');
    } else {
        window.alert('Too bad');
    }

</script>
T. Junghans
  • 11,385
  • 7
  • 52
  • 75
  • i changed the code-still not working: private void AlertWithConfirmation() { Response.Write(""); – mikespiteri Aug 17 '11 at 06:49
  • What does your output, the actually source code of your html file look like? – T. Junghans Aug 17 '11 at 08:16
1

The HTML generated by an aspx page is rendered in the Render phase which is at the end of the page lifecycle.

Therefore if you call Response.Write earlier in the page lifecycle, it will output at the start of the page before the first tag - almost certainly not what you want.

If you inspect the generated HTML (View Source in the browser) you'll see this.

In general, if you want to render some javascript you should use some other technique, such as setting the Text property of a Literal control at the appropriate place in the page.

Joe
  • 122,218
  • 32
  • 205
  • 338
1

You have already asked two similar questions in a period of 24h. You got to have some patience.

how to use javascript alert so that user can choose

Javascript alert problem

Community
  • 1
  • 1