Response.Write(JavaScript)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bassem
    Contributor
    • Dec 2008
    • 344

    Response.Write(JavaScript)

    Hello everybody,
    I know it is an ordinary question, I tried a lot and searched but got nothing.
    I request another page (Default2.aspx) using JavaScript from (Default.aspx), get a response Response.Write( ) as a JavaScript then using document.write( ) to update the page.
    Code:
    <script language="javascript" type="text/javascript">
    var xmlHttpTopologyReceiver = GetXmlHttpObject();
    function GetXmlHttpObject()
    {
        var xmlHttp = null;
        try
          {
              // Firefox, Opera 8.0+, Safari
              xmlHttp = new XMLHttpRequest();
          }
        catch (e)
          {
              // Internet Explorer
              try
                {
                    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
              catch (e)
                {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
          }
        return xmlHttp;
    }
    function receiveData()
    {
        if(null == xmlHttpTopologyReceiver)
        {
            alert("Your browser does not support AJAX!");
            return;
        }
        
        var url = "Default2.aspx";
            
        xmlHttpTopologyReceiver.onreadystatechange = stateChanged;
        xmlHttpTopologyReceiver.open("GET", url, true);
        xmlHttpTopologyReceiver.send(null);
    }
    
    function [B]hola()[/B]
    {
        alert('Hola');
    }
    
    function stateChanged() 
    { 
        if (xmlHttpTopologyReceiver.readyState == 4)
        {         
            var data = xmlHttpTopologyReceiver.responseText;
            
            document.write(data);
        }
    }
    </script>
    code of Default2.aspx
    Code:
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<script language=\"javascript\" type=\"text/javascript\">[B]alert('Hello');[/B]</script>");
        }
    The problem is when I replace alert('Hello'); with hola(); it does not execute.
    Any help please! Thanks
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Two problems:
    1. Don't use document.write after the page has finished loading, otherwise it will overwrite the already loaded page.
    2. When requesting JavaScript code, you need to either eval it or dynamically include via script tags (by adding to the head).

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      Thanks a lot acoder for reply.
      Thanks for correcting me. I've simple question if you have a time.
      1. What other possibility can I do instead of document.write( ) ?
      2. I didn't understand well what you meant but I'm going t read about eval more, please if you can guide me with simple link in that.

      Thanks,
      Bassem

      Comment

      • Bassem
        Contributor
        • Dec 2008
        • 344

        #4
        Well I just read a simple implement to eval function I'm out of my PC now but I hope it will work for me, the problem as you helped me is document.write( ) and I don''t know another way to implement.
        I'll try more.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          1. so-called DOM scripting (manipulating the document tree by methods of the DOM API). a good starting point is MDC

          2. to execute Javascript code it has to be parsed by the browser beforehand (which is done on page load) any Javascript inserted later is not parsed and thus not available by default.

          Comment

          • Bassem
            Contributor
            • Dec 2008
            • 344

            #6
            Thanks a lot Dormilich.
            I know my questions were so basic, but I'm getting my first steps and so lost.
            I'll follow the link.
            Thank you

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by Bassem
              2. I didn't understand well what you meant but I'm going t read about eval more, please if you can guide me with simple link in that.
              eval() is not the only option. If you're sure exactly what code will be produced, then eval might be ok, but if not, it could be dangerous. As I mentioned, you can also include the code using script tags dynamically, e.g.:
              Code:
              var script = document.createElement("script");
              script.type = ...
              script.appendChild(document.createTextNode(theCode);
              document.getElementsByTagName("head")[0].appendChild(script);

              Comment

              Working...