JavaScript没有被触发的参数 [英] JavaScript w/ argument not fired

查看:74
本文介绍了JavaScript没有被触发的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JS功能未被触发。我的应用程序中的2段代码如下:

My JS function is not fired. The 2 pieces of code in my app are below:

string strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "ShowSubscribe(" + strUrl +")", true); 




function ShowSubscribe(url) {
    var ans = confirm("Do you want to subscribe ?");
    if (ans == true) { //Redirect to other page for subscription.
        window.location = url + "ResetPassword.aspx";
    } else {
        //alert("You have click the cancel button.");
    }
}



我知道这有什么问题代码行:


I know something is wrong on this line of code:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "msg", "ShowSubscribe(" + strUrl +")", true);



如何纠正?谢谢。


How can it be corrected? Thanks.

推荐答案

这个问题的答案已在评论部分中给出,但我想补充一些其他内容。



有什么问题,你的JavaScript期待一个类似字符串的数据,但是你传递了一个令牌;可变等。如果你看一下它在控制台中被记录为未定义。



解决方法是在双引号内使用'';将连接数据转换为字符串。



The answer to this question has been given inside the comments section, but I would like to add a few more things to that.

What problem was, that your JavaScript was expecting a string-like data to work on, but you were passing a token; variable-like. Which would have been logged as undefined in your console if you would have a look at it.

The solution to it, is to use '' inside your double quotes; to convert the concatenated data as a string.

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), 
              "msg", "ShowSubscribe('" + strUrl +"')", true);
              // Would yield ShowSubscribe("value of strUrl")





另一个提示 [ ^ ],在这些情况下,您会错过这些连接和其他此类小错误,并不断抓挠您的注意事项。字符串连接总是会导致问题,要么将参数传递给字符串。格式() [ ^ ],或者你应该使用 StringBuilder [ ^ ]您将使用大量连接。



There is another tip[^] that I have posted for similar conditions, where you miss these concatenations and other such minor mistakes and keep scratching your head to notice where you missed it. String concatenations always cause problems, either pass parameters to String.Format()[^], or you should use StringBuilder[^] where you are going to use a lot of concatenations.


这篇关于JavaScript没有被触发的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆