简体   繁体   中英

Passing javascript parameter from codebehind error

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.

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

删除脚本标记,它将工作,这对我有用:

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

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.

Replace your msg varriable. Use this one---

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM