Connecting Tech Pros Worldwide Forums | Help | Site Map

Response.Write(JavaScript)

Familiar Sight
 
Join Date: Dec 2008
Posts: 202
#1: Jun 29 '09
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.
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript" type="text/javascript">
  2. var xmlHttpTopologyReceiver = GetXmlHttpObject();
  3. function GetXmlHttpObject()
  4. {
  5.     var xmlHttp = null;
  6.     try
  7.       {
  8.           // Firefox, Opera 8.0+, Safari
  9.           xmlHttp = new XMLHttpRequest();
  10.       }
  11.     catch (e)
  12.       {
  13.           // Internet Explorer
  14.           try
  15.             {
  16.                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  17.             }
  18.           catch (e)
  19.             {
  20.                 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  21.             }
  22.       }
  23.     return xmlHttp;
  24. }
  25. function receiveData()
  26. {
  27.     if(null == xmlHttpTopologyReceiver)
  28.     {
  29.         alert("Your browser does not support AJAX!");
  30.         return;
  31.     }
  32.  
  33.     var url = "Default2.aspx";
  34.  
  35.     xmlHttpTopologyReceiver.onreadystatechange = stateChanged;
  36.     xmlHttpTopologyReceiver.open("GET", url, true);
  37.     xmlHttpTopologyReceiver.send(null);
  38. }
  39.  
  40. function hola()
  41. {
  42.     alert('Hola');
  43. }
  44.  
  45. function stateChanged() 
  46.     if (xmlHttpTopologyReceiver.readyState == 4)
  47.     {         
  48.         var data = xmlHttpTopologyReceiver.responseText;
  49.  
  50.         document.write(data);
  51.     }
  52. }
  53. </script>
code of Default2.aspx
Expand|Select|Wrap|Line Numbers
  1.     protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         Response.Write("<script language=\"javascript\" type=\"text/javascript\">alert('Hello');</script>");
  4.     }
The problem is when I replace alert('Hello'); with hola(); it does not execute.
Any help please! Thanks

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jun 30 '09

re: Response.Write(JavaScript)


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).
Familiar Sight
 
Join Date: Dec 2008
Posts: 202
#3: Jul 2 '09

re: Response.Write(JavaScript)


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
Familiar Sight
 
Join Date: Dec 2008
Posts: 202
#4: Jul 2 '09

re: Response.Write(JavaScript)


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.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,652
#5: Jul 2 '09

re: Response.Write(JavaScript)


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.
Familiar Sight
 
Join Date: Dec 2008
Posts: 202
#6: Jul 2 '09

re: Response.Write(JavaScript)


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
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Jul 3 '09

re: Response.Write(JavaScript)


Quote:

Originally Posted by Bassem View Post

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.:
Expand|Select|Wrap|Line Numbers
  1. var script = document.createElement("script");
  2. script.type = ...
  3. script.appendChild(document.createTextNode(theCode);
  4. document.getElementsByTagName("head")[0].appendChild(script);
Reply