0

I have an html page that using ajax to call HttpHandler which needs to return javascript function back to client, so, when button is clicked this function gets executed.

I'm learning ajax now and using old fashion way to call it.

Here is my .html file:

<script language="javascript">
    var XMLHttpRequestObject = false;

    try {
        XMLHttpRequestObject = new ActiveXObject("MSXML2.XMLHTTP");
    } catch (exception1) {
        try {
            XMLHttpRequestObject = new ActiveXObject("Microsoft.MLHTTP");
        }
        catch (exception2) {
            XMLHttpRequestObject = false;
        }
    }

    if (!XMLHttpRequestObject && window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    }

    function getData(dataSource) {
        if (XMLHttpRequestObject) {
            XMLHttpRequestObject.open("GET", dataSource);

            XMLHttpRequestObject.onreadystatechange = function () {
               if (XMLHttpRequestObject.readyState == 4 
                    && XMLHttpRequestObject.status == 200)  {
                        eval(XMLHttpRequestObject.responseText);
               }
            }
            XMLHttpRequestObject.send(null);
        }
    }

    function alerter() {
        var targetDiv = document.getElementById("targetDiv");
        targetDiv.innerHTML = "Got the JavaScript OK.";
    }

</script>
</head>
 <body>
    <h1>Returning JavaScript</h1>
  <form>
      <input type="button" value="Fetch JavaScript" onclick="getData('http://myserver/DataSources/data.ashx')" />
  </form>
  <div id="targetDiv">
      <p>The fetched data will go here.</p>
  </div>
</body>

Than I have my HttpHandler where I need to register client script to call the function:

public class data1 : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        EchoMessage(context);   
    }

    private void EchoMessage(HttpContext context)
    {

    }
 } 

What should I write in my EchoMessage to register the script needed to run on a client side?

Thank you

eugene.it
  • 417
  • 3
  • 13
  • 32

3 Answers3

0

You can use RegisterStartupScript or RegisterClientScriptBlock to declare a script on the server and pass it on to be executed on the client. These are static functions of ClientScript and ScriptManager.

Differences between ScriptManager and ClientScript when used to execute JS?

Difference between RegisterStartupScript and RegisterClientScriptBlock?

Community
  • 1
  • 1
IanSoc
  • 238
  • 1
  • 3
  • 14
0

After all, I just decided to go with context.Response.Write("alerter()") and it seems to be working. Not sure if this is the right solution though

eugene.it
  • 417
  • 3
  • 13
  • 32
0

You can simply use the ScriptManager.RegisterStartupScript, which worked for me as below:

Page page = HttpContext.Current.Handler as Page;
ScriptManager.RegisterStartupScript(page, this.GetType(), "ErrorSave", "$cmsj('.alert-label').html('Maximum 4 quick links are allowed.');", true);
Keshab
  • 226
  • 2
  • 3
  • 14