C# ASP.net  - Java script

ukerandi
ukerandi used Ask the Experts™
on
How to write this using C# ASP.net page load event
var script = @"<script type="text/javascript">
    //<![CDATA[
    var GlvDelayedNextPageNo;

    function WebForm_CallbackComplete_SyncFixed() {
        // the var statement ensure the variable is not global
        for (var i = 0; i < __pendingCallbacks.length; i++) {
            callbackObject = __pendingCallbacks[i];
            if (callbackObject && callbackObject.xmlRequest &&
                (callbackObject.xmlRequest.readyState == 4)) {
                // SyncFixed: line move below // WebForm_ExecuteCallback(callbackObject);
                if (!__pendingCallbacks[i].async) {
                    __synchronousCallBackIndex = -1;
                }
                __pendingCallbacks[i] = null;
                var callbackFrameID = "__CALLBACKFRAME" + i;
                var xmlRequestFrame = document.getElementById(callbackFrameID);
                if (xmlRequestFrame) {
                    xmlRequestFrame.parentNode.removeChild(xmlRequestFrame);
                }
                // SyncFixed: the following statement has been moved down from above;
                WebForm_ExecuteCallback(callbackObject);
            }
        }
    }

    var OnloadWithoutSyncFixed = window.onload;

    window.onload = function Onload() {
        if (typeof (WebForm_CallbackComplete) == "function") {
            // Set the fixed version
            WebForm_CallbackComplete = WebForm_CallbackComplete_SyncFixed;
            // CallTheOriginal OnLoad
            if (OnloadWithoutSyncFixed != null) OnloadWithoutSyncFixed();
        }
    }
    //]]>
";

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Carl TawnSenior Systems and Integration Developer
Commented:
If you just mean "how do I flush some javascript to the browser", then you would use something like:
string script = "<script language=\"javascript\">alert('hello world');</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", script, false);

Open in new window

Just a minor update from Carl Tawn comment  in order to support multiline coding :
https://msdn.microsoft.com/en-us/library/aa479011.aspx
Page.RegisterClientScriptBlock("MyScript",
           "<script language=javascript>" +
           "if (document.images) {" +
           "MyButton = new Image;" +
           "MyButtonShaded = new Image;" +
           "MyButton.src = 'button1.jpg';" +
           "MyButtonShaded.src = 'button2.jpg';" +
           "}" +
           "else {" +
           "MyButton = '';" +
           "MyButtonShaded = '';" +
           "}" +
           "</script>");

Open in new window


IMO, it would be better if you code this in a sperate script(.js) file instead of embedding in  C#.net coding.

Regards
Rajar

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial