Jump to content
MakeWebGames

Javascript + Ajax


Haunted Dawg

Recommended Posts

Hello there, i need a little script that will refresh the <div> tag's.

I aint so good in javascript nor ajax so i was wondering if you could help me.

There is no board in here for ajax, so i put it here.

This is my current script:

 

	echo '
	<div id="shoutbox">
		<iframe 
			name="shoutbox" 
			marginwidth="0" 
			marginheight="0" 
			border="0" 
			frameborder="0" 
			table-align="center" 
			height="400" 
			width="100%" 
			scrolling="auto" 
			src="shoutbox">
		</iframe>
	</div>';

 

Can anyone help me?

Link to comment
Share on other sites

Re: Javascript + Ajax

Easier just do put something in the page the iframe is opening:

 

<script type="text/javascript" language="javascript">
function jsreload () {
window.location.reload();
}
setTimeout('jsreload()', 5000);
</script>

 

That reloads every 5 seconds.

Or you could use AJAX:

 

<script type="text/javascript" language="javascript">
var xmlHttp; 
function GetXmlHttpObject(){ 
var objXMLHttp=null; 
if (window.XMLHttpRequest){ 
objXMLHttp=new XMLHttpRequest(); 
}else if (window.ActiveXObject){ 
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
return objXMLHttp; 
} 
function update()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	return;
}
xmlHttp.onreadystatechange=function()
{
	if(xmlHttp.readyState==4 && xmlHttp.status == 200)
	{
		var response = xmlHttp.responseText;
		if (response)
		{
			document.getElementById('shoutbox').innerHTML=response;
		}
	}
}
//change the url to the chatbox file
var url="shoutbox.php";
xmlHttp.open("get", url+ '?ms=' + new Date().getTime());
xmlHttp.send(null);
}
//5 seconds
setInterval("update()", 5000);
</script>

Change the url variable to the shoutbox feed

This would mean to change the div tag to:

<div id="shoutbox" style=" height:400px; width:98%; overflow:scroll;">
</div>
Link to comment
Share on other sites

Re: Javascript + Ajax

Where would i put this bit of code:

 

<script type="text/javascript" language="javascript">
var xmlHttp; 
function GetXmlHttpObject(){ 
var objXMLHttp=null; 
if (window.XMLHttpRequest){ 
objXMLHttp=new XMLHttpRequest(); 
}else if (window.ActiveXObject){ 
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
return objXMLHttp; 
} 
function update()
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	return;
}
xmlHttp.onreadystatechange=function()
{
	if(xmlHttp.readyState==4 && xmlHttp.status == 200)
	{
		var response = xmlHttp.responseText;
		if (response)
		{
			document.getElementById('shoutbox').innerHTML=response;
		}
	}
}
//change the url to the chatbox file
var url="shoutbox.php";
xmlHttp.open("get", url+ '?ms=' + new Date().getTime());
xmlHttp.send(null);
}
//5 seconds
setInterval("update()", 5000);
</script>

 

And i never knew there was a ajax board.

Link to comment
Share on other sites

Re: Javascript + Ajax

This is my current code in the file.

 

<?php //Added <?php tags to make it look neater

function _3()
{
global $ir,$h,$set,$data;
echo '[b]Shoutbox Room[/b]

';
if(file_exists("shoutbox.php"))
{
	echo '
	<div id="ShoutBox.Room" style=" height:400px; width:98%; overflow:scroll;">
		<script type="text/javascript" language="javascript">
			var xmlHttp; 
			function GetXmlHttpObject()
			{ 
				var objXMLHttp=null; 
				if (window.XMLHttpRequest)
				{ 
					objXMLHttp=new XMLHttpRequest(); 
				}
				else if (window.ActiveXObject)
				{ 
					objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
				} 
				return objXMLHttp; 
			} 
			function update()
			{
				xmlHttp=GetXmlHttpObject();
				if (xmlHttp==null)
				{
					return;
				}
				xmlHttp.onreadystatechange=function()
				{
					if(xmlHttp.readyState==4 && xmlHttp.status == 200)
					{
						var response = xmlHttp.responseText;
						if (response)
						{
							document.getElementById("ShoutBox.Room").innerHTML=response;
						}
					}
				}
				//change the url to the chatbox file
				var url="shoutbox.php";
				xmlHttp.open("get", url+ "?ms=" + new Date().getTime());
				xmlHttp.send(null);
			}
			//5 seconds
			setInterval("update()", 5000);
		</script>
		<iframe 
			name="Shoutbox Room" 
			marginwidth="0" 
			marginheight="0" 
			border="0" 
			frameborder="0" 
			table-align="center" 
			height="400" 
			width="100%" 
			scrolling="auto" 
			src="shoutbox.php">
		</iframe>
	</div>';
}
else
{
	echo 'Shoutbox Currently Down.';
}
_back();
}

?>

 

Sorry if i am putting it in the wrong place, but unfortunatly im not very aware of how ajax or javascript work.

Link to comment
Share on other sites

Re: Javascript + Ajax

....should be:

<?php //Added <?php tags to make it look neater

function _3()
{
global $ir,$h,$set,$data;
echo '[b]Shoutbox Room[/b]

'
if(file_exists("shoutbox.php"))
{
echo '
		<script type="text/javascript" language="javascript">
			var xmlHttp; 
			function GetXmlHttpObject()
			{ 
				var objXMLHttp=null; 
				if (window.XMLHttpRequest)
				{ 
					objXMLHttp=new XMLHttpRequest(); 
				}
				else if (window.ActiveXObject)
				{ 
					objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
				} 
				return objXMLHttp; 
			} 
			function update()
			{
				xmlHttp=GetXmlHttpObject();
				if (xmlHttp==null)
				{
					return;
				}
				xmlHttp.onreadystatechange=function()
				{
					if(xmlHttp.readyState==4 && xmlHttp.status == 200)
					{
						var response = xmlHttp.responseText;
						if (response)
						{
							document.getElementById("ShoutBox.Room").innerHTML=response;
						}
					}
				}
				//change the url to the chatbox file
				var url="shoutbox.php";
				xmlHttp.open("get", url+ "?ms=" + new Date().getTime());
				xmlHttp.send(null);
			}
			//5 seconds
			setInterval("update()", 5000);
		</script>
             <div id="ShoutBox.Room" style=" height:400px; width:98%; overflow:scroll;">
	</div>';
}
else
{
	echo 'Shoutbox Currently Down.';
}
_back();
}

?>

 

Then you need a file called shoutbox.php to output the shoutbox data.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...