[英]Calling javascript function from codebehind error
I am trying to call one of my javascript functions that loaded in my master page from an aspx page. 我试图从aspx页面调用我的主页中加载的一个javascript函数。
Below is the code i am currently using: 以下是我目前使用的代码:
ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "showNotifier", "showNotifier(3000,'green','test');", True)
The javascript code is this: javascript代码是这样的:
function showNotifier(delayTime, color, theMsg) {
var theDivName = '#Notifier';
e.preventDefault();
$(theDivName).css("background", color);
$(theDivName).text(theMsg);
$(theDivName).animate({ top: 0 }, 300, null);
$(theDivName).css("position", "fixed");
//Hide the bar
$notifyTimer = setTimeout(function () {
$(theDivName).animate({ top: -100 }, 1500, function () {
$(theDivName).css("position", "absolute");
});
}, delayTime);
});
And this is where its being called in my code behind: 这就是我的代码背后调用的地方:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ScriptManager.RegisterStartupScript(Me.Page, GetType(String), "showNotifier", "showNotifier(3000,'green','test');", True)
End If
End Sub
When i run my page and it loads up that aspx page, it displays this error: 当我运行我的页面并加载该aspx页面时,它会显示以下错误:
Microsoft JScript runtime error: 'showNotifier' is undefined.
What would be causing this? 会导致什么?
Use RegisterClientScriptBlock
instead. 请改用
RegisterClientScriptBlock
。
And also wrap your call in $(function() { ... } ).: 并且还将您的调用包装在$(function(){...})中:
;$(function() {showNotifier(3000,'green','test');});
UPDATE : So your VB code will look like so: 更新 :所以你的VB代码看起来像这样:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "showNotifier", ";$(function() {showNotifier(3000,'green','test');});", True)
End If
End Sub
UPDATE 2 Your js function has some syntax errors: 更新2您的js函数有一些语法错误:
function showNotifier(delayTime, color, theMsg) {
var theDivName = '#Notifier';
// 'e' is not declared -- e.preventDefault();
$(theDivName).css("background", color);
$(theDivName).text(theMsg);
$(theDivName).animate({ top: 0 }, 300, null);
$(theDivName).css("position", "fixed");
//Hide the bar
$notifyTimer = setTimeout(function () {
$(theDivName).animate({ top: -100 }, 1500, function () {
$(theDivName).css("position", "absolute");
});
}, delayTime);
}// ); - this should not be here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.