asp.net - Javascript error: function XXXX() not defined - Stack Overflow

I added a javascript function in a page <head><script type=textjavascript>function show_Al

I added a javascript function in a page

 <head>
    <script type=text/javascript>
        function show_Alert(error)
        {
           alert(error);
         }
    </script>
</head>

and on button click I am doing this

Protected void btn_Click(object o,Eventargs e)
{
    StringBuilder str = new StringBuilder();
    str.AppendLine("show_Alert('XYZ error')");
    ClientScript.RegisterStartupScript(GetType(),"Alert",str.ToString(),true);
}

But it throws JS error show_Alert is not defined :(

Any Idea, what is wrong here??

Thanx

I added a javascript function in a page

 <head>
    <script type=text/javascript>
        function show_Alert(error)
        {
           alert(error);
         }
    </script>
</head>

and on button click I am doing this

Protected void btn_Click(object o,Eventargs e)
{
    StringBuilder str = new StringBuilder();
    str.AppendLine("show_Alert('XYZ error')");
    ClientScript.RegisterStartupScript(GetType(),"Alert",str.ToString(),true);
}

But it throws JS error show_Alert is not defined :(

Any Idea, what is wrong here??

Thanx

Share Improve this question edited Dec 23, 2010 at 14:08 BreakHead asked Dec 23, 2010 at 14:04 BreakHeadBreakHead 10.7k39 gold badges119 silver badges169 bronze badges 1
  • Sorry That was not a issue, I am editing question – BreakHead Commented Dec 23, 2010 at 14:07
Add a ment  | 

3 Answers 3

Reset to default 4

Your script tag is wrong.
Change it to

<script type="text/javascript">

However, I don't think that's the issue.
I suspect that RegisterStartupScript is emitting its <script> block before the one with your function, so that it ends up calling the function before it exists.
Check where each <script> is in the rendered source.

Make sure your <script> element is valid, like this:

<head>
  <script type="text/javascript">
    function show_Alert(error)
    {
      alert(error);
    }
  </script>
</head>

If it's not well formed, or the type is unrecognized (your case has both) then the script inside will be ignored, since the browser doesn't know how to handle it.

Here's an example:

<%@ Page Language="C#" %>
<script type="text/C#" runat="server">
    protected void BtnClick(object sender, EventArgs e)
    {
        var str = "XYZ Error";
        var script = new StringBuilder();
        script.AppendFormat("showAlert('{0}');",
            HttpUtility.JavaScriptStringEncode(str)
        );
        ClientScript.RegisterStartupScript(GetType(), "alert", script.ToString(), true);
    }
</script>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript">
        function showAlert(error) {
            alert(error);
        }    
    </script>
</head>
<body>
    <form id="Form1" runat="server">
        <asp:LinkButton ID="Btn" runat="server" Text="Click me" OnClick="BtnClick" />
    </form>
</body>
</html>

A very important thing to pay attention to is to properly encode the string you are passing to the showAlert function. Notice that it is encoded:

script.AppendFormat("showAlert('{0}');",
    HttpUtility.JavaScriptStringEncode(str)
);

If you don't encode it and the string contains some special characters like ' for instance, your script will break.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287540a4415595.html

相关推荐

  • asp.net - Javascript error: function XXXX() not defined - Stack Overflow

    I added a javascript function in a page <head><script type=textjavascript>function show_Al

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信