Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

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.