Connecting Tech Pros Worldwide Help | Site Map

Response.Write(JavaScript)

  #1  
Old June 29th, 2009, 08:20 PM
Familiar Sight
 
Join Date: Dec 2008
Posts: 200
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
  #2  
Old June 30th, 2009, 11:20 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

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).
  #3  
Old July 2nd, 2009, 07:33 PM
Familiar Sight
 
Join Date: Dec 2008
Posts: 200

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
  #4  
Old July 2nd, 2009, 07:44 PM
Familiar Sight
 
Join Date: Dec 2008
Posts: 200

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.
  #5  
Old July 2nd, 2009, 07:45 PM
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,485
Provided Answers: 9

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.
  #6  
Old July 2nd, 2009, 09:29 PM
Familiar Sight
 
Join Date: Dec 2008
Posts: 200

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
  #7  
Old July 3rd, 2009, 11:46 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
validating dynamically created radio buttons using Response.write satyabhaskar answers 5 March 2nd, 2009 02:18 PM
response.write on postback =?Utf-8?B?SmFzb24gSGFydHNvZQ==?= answers 3 September 25th, 2008 01:55 PM
page layout changes after response.write Filip De Backer answers 4 May 8th, 2006 06:05 PM
Response.Write and Response.Redirect Matthew Wieder answers 4 November 17th, 2005 07:00 PM