473,418 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Response.Write(JavaScript)

344 100+
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
6 20493
acoder
16,027 Expert Mod 8TB
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
Bassem
344 100+
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
Bassem
344 100+
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
8,658 Expert Mod 8TB
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
Bassem
344 100+
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
16,027 Expert Mod 8TB
@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

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

Similar topics

4
by: Matthew Wieder | last post by:
On my Page_Load event, i need to do some validation and then either let them proceed, or display a error message and boot them back to the previous page. Here is the code: ...
8
by: Paul | last post by:
Hi, my problem is that I can't get getElementById to find the ID of any controls on my page. To whit: The following code-behind returns the error, "lblErrorText is undefined.": ...
4
by: Nevyn Twyll | last post by:
Is there any way I can use a code-behind event (like a btn_Click event) to write some javascript into the Response? I was thinking of using Response.Write() to write the following code into the...
1
by: Asha | last post by:
hello, from my main page, i have an iframe which calls another .aspx file; and the other .aspx file will response.write values to it. but i keep getting the below message. can someone pleasehelp me...
2
by: Charles A. Lackman | last post by:
Hello, I have an ASPX page that is storing data into a session Variable: Session("Name") = "Chuck" When the User clicks "Next" on the page, it naviages to "Details.aspx", Details.aspx has two...
4
by: Filip De Backer | last post by:
Hi everyone, string script = ""; script += "<script language='javascript'>"; script += "window.open('showdoc.aspx');"; script += "</script>"; Response.Write(script); After the execution of...
1
by: Tiago Vieira | last post by:
Hi everybody, I'm trying to insert a piece of javascript inside of a web service method but It's not working. Have somebody already tryed to do it? Folow a piece of my code below. ---------...
3
by: =?Utf-8?B?SmFzb24gSGFydHNvZQ==?= | last post by:
I get the error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by...
5
by: satyabhaskar | last post by:
hi all, In my web page i have created radio buttons dynamically on to the page .....following is my code string Course, Semester, Section; int rowsCount; string con =...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.