View Single Post
  #1 (permalink)  
Old 07-15-08, 08: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 04:03 AM. Reason: fixed code highlighters
Reply With Quote