DeathStar Posted June 20, 2007 Share Posted June 20, 2007 Hello. I need a way, in php to automatically run a js function when the page is entered. I cannot use onload, because I need it to run be fore anything loads. Any ideas? Quote Link to comment Share on other sites More sharing options...
king arthur Posted June 20, 2007 Share Posted June 20, 2007 JS is a client side language - it runs in the browser, so the page has to load for the browser to be able to parse it. What is it you're trying to do? Quote Link to comment Share on other sites More sharing options...
DeathStar Posted June 20, 2007 Author Share Posted June 20, 2007 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() Quote Link to comment Share on other sites More sharing options...
mainewoods Posted June 25, 2007 Share Posted June 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.