Jump to content

Do a js function before the page loads.


DeathStar

Recommended Posts

Well, I have been trying to make an ajax loading screen, I have it working, just not automatticly loading.

I got it closing, Just need it to load.

Here is the ajax(its from a tutorial, then edited):

var LoadingScreen = {

hide: function() {
	var div = document.getElementById('loading_message');
	if (div == null || div == '' || typeof(div) != 'object') { return false; }

	document.body.removeChild(div);
},

moving_dots: function() {
	var div = document.getElementById('loading_message');
	if (div == null || div == '' || typeof(div) != 'object') { return false; }

	var html = div.innerHTML;

	// Add dot or start over?
	var num_dots = html.split('.').length-1;
	if (num_dots > 6) {
		div.innerHTML = '<b>Please wait.</b><br />Loading';
	} else {
		div.innerHTML = html + '.';
	}

},


set_position: function () {
	var div = document.getElementById('loading_message');
	if (div == null || div == '' || typeof(div) != 'object') { return false; }

	var scrollTop = LoadingScreen.f_scrollTop();
	var scrollLeft = LoadingScreen.f_scrollLeft();
	var clientWidth = LoadingScreen.f_clientWidth();

	// Calculate left position
	var left = clientWidth - div.offsetWidth;
	left = scrollLeft + left;

	// Set position
	div.style.position = 'absolute';
	div.style.left = left + 'px';
	div.style.top = scrollTop + 'px';
},

create_div: function () {
	var div = document.createElement('DIV');
	div.id = 'loading_message';

	div.innerHTML = '<b>Please wait.</b><br />Loading';

	var css = "border: 1px solid black;"
	css += "font-size: 10px;"
	css += "font-family: Tahoma;"
	css += "width: 100px;";
	css += "padding: 10px;";
	css += "background-color: #FFFFFF;";
	css += "color: black;";
	css += "font-weight: none;";

	div.style.cssText = css;

	document.body.appendChild(div);

	return div;
},

f_clientWidth: function () {
	return this.f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
},

f_scrollLeft: function () {
	return this.f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
},

f_scrollTop: function () {
	return this.f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
},

f_filterResults: function (n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
	n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
},

addEventHandler: function (oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	} 
}

}
function load_show(){
	// Create Loading div
	LoadingScreen.create_div();

	// Set initial position				
	LoadingScreen.set_position();

	// Attach event handler to onScroll to keep in position
	LoadingScreen.addEventHandler(window, 'scroll', LoadingScreen.set_position);

	// Start the moving dots
	setInterval(LoadingScreen.moving_dots, 500);
}

 

To start I use the load_show() function.

To hide, LoadingScreen.hide()

Link to comment
Share on other sites

hide: function() {
	var div = document.getElementById('loading_message');

any js that uses getElementById()  has to wait for page load because the dom element it is trying to access will not exist until the page is parsed.  Trying to access a dom element before it exists will result in a js error

 

Link to comment
Share on other sites

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.