0

I have a javascript function like

function CloseWindow() {
    alert("I am firing");    // window.close();         
}

I am trying to call it from code behind button click event like

Page.ClientScript.RegisterStartupScript([GetType](), "Javascript", "javascript:CloseWindow();", True)

The function is not firing alert message. If I call the same function from OnClientClick it is firing.

OnClientClick="javascript:();"

What might be the cause? If you want anymore clarification, please let me know.

by <script type="text/javascript" language="javascript"> function CloseWindow() { alert("I am firing"); // window.close();
} function chkLength(evt, len) { var str = document.getElementById(evt.id); if (str.value.length < len) return true; else return false; } </script>

3
  • Can you provide the code surrounding the javascript code? Commented Mar 5, 2014 at 9:35
  • I tagged with VB.Net, everyone is answering in c# :( Commented Mar 5, 2014 at 9:39
  • @Johan Check my Script section. Commented Mar 5, 2014 at 9:44

3 Answers 3

4

If you Used Update Panels Then You can Use:

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "javascriptFunction();", true);

Other Wise You can Use

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascriptFunction();",true);
Sign up to request clarification or add additional context in comments.

Comments

2

try the below code....

ClientScript.RegisterStartupScript
        (GetType(),Guid.NewGuid().ToString(), "javascript: CloseWindow();",true);

1 Comment

Is it true in VB.Net or True?
0

The third parameter (script) must be "CloseWindow();" and not "javascript:CloseWindow();"

1 Comment

Be sure that the function "CloseWindow" is declared at the very top of your html page.

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.