By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,685 Members | 1,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,685 IT Pros & Developers. It's quick & easy.

Response.Write(JavaScript)

100+
P: 344
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
Jun 29 '09 #1
Share this Question
Share on Google+
6 Replies


acoder
Expert Mod 15k+
P: 16,027
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).
Jun 30 '09 #2

100+
P: 344
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
Jul 2 '09 #3

100+
P: 344
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.
Jul 2 '09 #4

Dormilich
Expert Mod 5K+
P: 8,639
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.
Jul 2 '09 #5

100+
P: 344
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
Jul 2 '09 #6

acoder
Expert Mod 15k+
P: 16,027
@Bassem
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);
Jul 3 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.