Current location: Hot Scripts Forums » General Web Coding » JavaScript » Calling javascript through ajax


Calling javascript through ajax

Reply
  #1 (permalink)  
Old 07-15-08, 09:58 PM
webdevised webdevised is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Calling javascript through ajax

I have a quick question about my ajax script..I have a script that loads the content of a another page into a div..

ajax.htm
HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>An XHTML 1.0 Strict standard template</title>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<style type="text/css">

	</style>
	<script type="text/javascript">
	function ajaxFunction(id, url){
		var xmlHttp;
		try {// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();		
		} catch (e) {// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					alert("Your browser does not support AJAX!");
					return false;
				}
			}
		}
		
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
				//Get the response from the server and extract the section that comes in the body section of the second html page avoid inserting the header part of the second page in your first page's element
				var respText = xmlHttp.responseText.split('<body>');
				elem.innerHTML = respText[1].split('</body>')[0];
			}
		}

		var elem = document.getElementById(id);
		if (!elem) {
			alert('The element with the passed ID doesn\'t exists in your page');
			return;
		}
	
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}		
</script>
</head>

<body>
	<div id="test"></div>
	<form>
		<input type="button" value="Make Ajax Call" id="ajax" onclick="ajaxFunction('test','time.asp');"/>
	</form>
</body>
</html>
time.asp
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<%
response.write("hello from asp")
%>

<script type="text/javascript">
document.write("Hello from javascript");
</script>
</body>
</html>
I can get any thing to load in the time.asp page but the javascript..

The reason i need this is that i have a box on my page that when someone clicks it it will load the content from the asp page and use a javascript counter to count page views..

Any thoughts??

Thanks a head of time..

Last edited by UnrealEd; 07-16-08 at 05:03 AM. Reason: fixed code highlighters
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-16-08, 05:30 AM
Vicious's Avatar
Vicious Vicious is offline
Community VIP
 
Join Date: Jan 2007
Location: Belgium
Posts: 584
Thanks: 0
Thanked 0 Times in 0 Posts
Javascript you get via an AJAX call won't be evaluated.
You'll have to find a solution where you put the code from time.asp into the first page in a function. When your AJAX call is finished, you then call that function.
__________________
Jack Bauer makes Chuck Norris cry
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-16-08, 01:00 PM
webdevised webdevised is offline
Newbie Coder
 
Join Date: Sep 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
I used the eval code from http://www.w3schools.com/jsref/jsref_eval.asp

my new code for time.asp is this

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<%
response.write("hello from asp")
%>

<script type="text/javascript">
eval("alert('Hello world')");
</script>
</body>
</html>
now this works.. but how can i call that alert from an extranal js file..??

--------------------------------------------------------------
like on test.js i have this.
HTML Code:
eval("alert('Hello world')");
in time.asp i have this.

HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<%
response.write("hello from asp")
%>

<script type="text/javascript" src="test.js"></script>
</body>
</html>

this doesn't work.. ummmm

Last edited by webdevised; 07-16-08 at 01:02 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-16-08, 01:06 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
You can't do that. I suggest putting your response into a XML document, eg:
xml Code:
  1. <response>
  2.     <alert>Hello world!</alert>
  3.     <html><![CDATA[ HTML HERE ]]></html>
  4.     <!-- Or: -->
  5.     <script>myscript.js</script>
  6. </response>
... and then you could parse each part separately. Or even dynamically add a script tag to the header and load the URL you specified in the XML document.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading Javascript from AJAX Responce scott2500uk JavaScript 3 01-24-08 12:46 PM
How to recive mysql data using javascript ajax? method JavaScript 15 03-23-07 12:38 PM
Regarding calling a codebehind method in c# using javascript vishalsharma.net ASP.NET 1 08-31-06 07:06 AM
help calling a JAvascript function sharijl JavaScript 3 02-12-06 02:31 PM
blog about Javascript, AJAX, JSON jemptymethod JavaScript 0 07-21-05 10:13 AM


All times are GMT -5. The time now is 12:32 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.