Public feed #892566

Unnamed

public text v1 · immutable
Raw Edit
·published 2008-02-05 15:20 UTC
rendered paste body
-----------------------html page------------------------------------
<html>
  <head>
    <script src="ajaxscript.js">
    </script>
  </head>
  <body>
    <h1>This is index page!</h1>
    <form name="frmTest" id="frmTest">
      <label>Some time</label>: <label id="lblRandomNum"></label>

      <br>
      <a href="javascript:popLabel( 'lblRandomNum' );">Get a random no!</label>
    </form>
  </body>
</html>

-----------------------ajaxscript.js--------------------------------

// Create the XMLHTTPRequest Object
function getXHRObject() {
    var httpRequest;
    
    if( window.XMLHttpRequest ) { // Mozilla, Safari, ...
	httpRequest = new XMLHttpRequest();
	if ( httpRequest.overrideMimeType ) {
	    httpRequest.overrideMimeType( 'text/xml' );
	}
    } 
    else if ( window.ActiveXObject ) { // IE
	try {
	    httpRequest = new ActiveXObject( "Msxml2.XMLHTTP" );
	} 
	catch( e ) {
	    try {
		httpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
	    } 
	    catch( e ) {}
	}
    }

    if ( !httpRequest ) {
	alert( 'Giving up :( Cannot create an XMLHTTP instance' );
	return false;
    }
    
    return( httpRequest );
}

function assignCallBack( httpRequest, callBackFunction, params )
{
    httpRequest.onreadystatechange = function(){ callBackFunction( httpRequest, params ) };
}

function callBackLabel( httpRequest, id )
{
    if( httpRequest.readyState == 4 ){
	document.getElementById( id ).innerHTML = httpRequest.responseText;
    }
    
}


function popLabel( id ) 
{
    xhr = getXHRObject();
    
    assignCallBack( xhr, callBackLabel, id );

    xhr.open( 'GET', "app.php?pg=giveTime&action=&do=&ts=" + Math.random(), true);
    xhr.send( null );
}