Response.Write(JavaScript) 
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. - <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 hola()
-
{
-
alert('Hola');
-
}
-
-
function stateChanged()
-
{
-
if (xmlHttpTopologyReceiver.readyState == 4)
-
{
-
var data = xmlHttpTopologyReceiver.responseText;
-
-
document.write(data);
-
}
-
}
-
</script>
code of Default2.aspx - protected void Page_Load(object sender, EventArgs e)
-
{
-
Response.Write("<script language=\"javascript\" type=\"text/javascript\">alert('Hello');</script>");
-
}
The problem is when I replace alert('Hello'); with hola(); it does not execute.
Any help please! Thanks
| 
June 30th, 2009, 11:20 AM
|  | 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).
| 
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
| 
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.
| 
July 2nd, 2009, 07:45 PM
|  | 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.
| 
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
| 
July 3rd, 2009, 11:46 AM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,517
Provided Answers: 12 | | | re: Response.Write(JavaScript) Quote:
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.: - var script = document.createElement("script");
-
script.type = ...
-
script.appendChild(document.createTextNode(theCode);
-
document.getElementsByTagName("head")[0].appendChild(script);
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|