Jump to content

How to run a script from another


MrBillybob

Recommended Posts

i dont want the script to run on my index because it take a long time...

 

That is unavoidable. If you really want to invoke an external script into the index, it's ultimately going to be processed as if it were on the index page anyway. Now it just depends whether you want it to happen -after-the rest of the page has loaded or -during- the page load.

 

If you want it during, you will use Dan's methods (Require/include/etc.), if you want it after, use AJAX to call in that PHP script.

Ok, I'm going to write this response assuming you understand basic javascript and how it interacts with HTML pages.

 

first of all, you need to import (or put it into the page directly) this javascript:

 

var xmlhttp = false;

function ajax_init()
{
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(ie)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(other)
		{
			xmlhttp = false;
		}
	}

	if( !xmlhttp && typeof XMLHttpRequest != 'undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
}

function ajax_request(method, url, nodeID)
{
	var node = document.getElementById(nodeID);
	xmlhttp.open(method, url, true);
	xmlhttp.onreadystatechange = function()
	{
		if( xmlhttp.readyState == 4 )
		{
			switch( xmlhttp.status )
			{
				case 200:
					node.innerHTML = xmlhttp.responseText;
					break;
				default:
					node.innerHTML = "There was a server error (" + xmlhttp.status + "), and it could not finish loading. Please try again.";
			}
		}
	}
	xmlhttp.send('ajax=1');
}

 

Now, ask yourself, does your external script print anything to the screen? Do you want that shown or not?

 

If it doesn't matter, then make an empty <div id="myDiv"></div> at the bottom of the page. (the id can be whatever you like, but I'm going to use myDiv in my examples)

 

If it IS important that the output is shown on the page, replace "myDiv" with the ID of the HTML element where you want it shown.

 

***

 

Now, on the <body> tag, change it to:

 

<body onload="ajax_init(); ajax_request("post", "externalScript.php", "myDiv");">

 

Of course, change "externalScript.php" to be your script file.

 

And there you go! It will call that script once the page has loaded.

 

Hopefully that'll work, it's 7am and I wrote that from memory.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.