0

I use this script to call a javascript from codebehind.

ClientScript.RegisterStartupScript(this.GetType(), "Exist", "<script language='javascript'>ConfirmRedirect('" + url + "','" + msg + "');</script>", true);

and my javascript code is

function ConfirmRedirect(url,msg)
    {    

        alert(msg);
        window.location.href = url;
    }

Am getting ')' expected error. What am missing here? If am calling javascript without paramters then it is working.

4 Answers 4

1

Remove the Script tag and it will work, this worked for me:

 ClientScript.RegisterStartupScript(this.GetType(), "Exist", "ConfirmRedirect('" + url + "','" + msg + "');", true);
Sign up to request clarification or add additional context in comments.

Comments

1

View the HTML source and include it in your question/post.

You have probably just not escaped the msg enough. It can for example not contain single quotes ' or line breaks.

UPDATE

A simple solution would be to escape all single quotes using Replace().

ClientScript.RegisterStartupScript(this.GetType(), "Exist", "<script language='javascript'>ConfirmRedirect('" + url + "','" + msg.Replace("'", "\'") + "');</script>", true);

2 Comments

<script type="text/javascript"> //<![CDATA[ <script language='javascript'>ConfirmRedirect('selectappraisee.aspx','Appraisal Details has been upated for ['M.P.Harimurugan'].');</script>//]]> </script> This is the HTML source am getting. How should I pass the parameter? //]]> why this characters coming in html source?
@itzArun That is because your msg string variable contains that text. You can escape them by adding a backslash in front of them like; \'.
0

Probably the msg parameter contain symbols like the '

Then the code will be something like

<script language='javascript'>
    ConfirmRedirect('/myurl/somthing.aspx','Here's my message');
</script>

And there wait for the close parenthesis.

3 Comments

string msg="Your Comments has been Updated" This is what I declared for msg.
@itzArun Then check for the same think on the url.
But am calling this script in many places. Like protected void RedirectMessage(string url,string msg) { ClientScript.RegisterStartupScript(this.GetType(), "Exist", "<script language='javascript'>ConfirmRedirect('" + url + "','" + msg + "');</script>", true); }
0

Replace your msg varriable. Use this one---

ClientScript.RegisterStartupScript(this.GetType(), "Exist", "javascript:ConfirmRedirect('" + url + "','" + msg.Replace("'","") + "');", true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.