0

I am trying to call one of my javascript functions that loaded in my master page from an aspx page.

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:

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:

Microsoft JScript runtime error: 'showNotifier' is undefined.

What would be causing this?

2
  • And where does showNotifier live? Is the code on the page? Commented Aug 10, 2012 at 19:20
  • @epascarello: showNotifier is a javascript file thats loaded up inside the master page. Commented Aug 10, 2012 at 19:28

1 Answer 1

2

Use RegisterClientScriptBlock instead.

And also wrap your call in $(function() { ... } ).:

;$(function() {showNotifier(3000,'green','test');});

UPDATE: So your VB code will look like so:

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:

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
Sign up to request clarification or add additional context in comments.

5 Comments

Says that RegisterClientScript is not a member of System.Web.UI.ScriptManager
Sorry, use RegisterClientScriptBlock. I've updated the answer
Still shows my original error of Microsoft JScript runtime error: 'showNotifier' is undefined.
Hmm... can you open the generated HTML and see that the function is defined there? Also, there is a bracket at the end of function - it should be removed.
Got it that time. It was because of the error in the javascript. Blah wish MS would tell me that instead of giving me that generic error!

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.