admin管理员组文章数量:1402808
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>");
}
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>");
}
Share
Improve this question
edited Aug 17, 2011 at 6:40
Guffa
701k111 gold badges756 silver badges1k bronze badges
asked Aug 17, 2011 at 6:36
mikespiterimikespiteri
9015 gold badges13 silver badges25 bronze badges
4
-
Some general things: It's
<script type="text/javascript">
, you don't have to prefix global functions withwindow.
, quote orgies are bad, use single quotes inside double-quotes strings whenever possible. – ThiefMaster Commented Aug 17, 2011 at 6:40 - @dlev i am testing it on page load and it is not working, however this will be used in the middle of another method after several if statements are executed. thanks – mikespiteri Commented Aug 17, 2011 at 6:40
- Do you get any script errors ? – V4Vendetta Commented Aug 17, 2011 at 6:42
- @V4Vendetta - i dont get any script errors – mikespiteri Commented Aug 17, 2011 at 6:50
4 Answers
Reset to default 3Your 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 es from the lack of separator between the mands, 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 deprecatedlangauge
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.
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>
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.
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
本文标签: cjavascript alert not workingStack Overflow
版权声明:本文标题:c# - javascript alert not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744366190a2602787.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论