0

I am trying to call a Javascript function from my code, but I'm getting the Object Expected js error.

My call to the function:

            ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type=\"text/JavaScript\">returnVal();</script>");

And my javascript from the .aspx

<script language ="javascript">
function returnVal() {
    var hidAppID = document.getElementById("hidAppId");
    var hidAppName = document.getElementById("hidAppName");
    var hidAppSox = document.getElementById("hidAppSox");

    if (window.showModalDialog) {
        var sharedApp = {};
        sharedApp.Id = hidAppID.value;
        sharedApp.Name = hidAppName.value;
        sharedApp.Sox = hidAppSox.value;

        window.returnValue = sharedApp;
    }
    window.close();
}
</script>

The javascript is valid - I added a temporary button to the page to test an "onclicked" call to it. Unfortunately, I need to call it from the code.

4
  • What's the generated source? Commented Jul 25, 2013 at 21:22
  • I'm not sure if this is what you are asking for: <script src="/ServiceRequest/WebResource.axd?d=7WM84Cl5TnKU_RJMH3HviqVFNhsBJZD1CgCdpmUUbwK95Ds3iKztpSGZ0NZJKRdPBAE2DiO33POpwVVSx_icHjCTkNY1&amp;t=634604208479085897" type="text/javascript"></script> <script type="text/JavaScript"> returnVal(); < Commented Jul 25, 2013 at 21:24
  • Do you see the problem now? Commented Jul 25, 2013 at 21:25
  • it looks like i have an empty tag there at the end. should have seen that before. Do I have an escape character in there somewhere? Commented Jul 25, 2013 at 21:28

2 Answers 2

1

Try calling it without embedded tags, but passing 3rd parameter instead:

ClientScript.RegisterClientScriptBlock(GetType(), "script", "returnVal();", true);
Sign up to request clarification or add additional context in comments.

6 Comments

I still get the error. The generated code is: <script type="text/javascript"> //<![CDATA[ returnVal();//]]> <
Can you put a breakpoint inside of that JS function ad see if it is getting called at all and if it is - which line throws the error?
I do have a breakpoint but it isn't getting hit. I tried the same code you posted above, but "RegisterStartupScript" instead. I got the same generated code with an addition: <script type="text/javascript"> //<![CDATA[ returnVal();Sys.Application.initialize(); //]]> <
When is this script generated in you server-side code? And where, relatively to Update Panel is function returnVal() located?
My hardcoded script is at the bottom of the page (outside html tags). And I'm not using an update panel on the page. I just double checked to make sure.
|
0

You have put \returnVal(); in the string, which makes \r the escape sequence for the carrage return character, so you will be trying to call the function eturnVal, which doesn't exist.

Remove the backslash from the code:

ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type=\"text/JavaScript\">returnVal();\n</script>");

1 Comment

Sorry that was just copied incorrectly. It has been corrected in the OP

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.