Don't show page if onLoad event takes page elsewhere

I have this code:
<body onLoad="save_proj(); chk_ems(); set_pointers(); chk_skip_summ();">

Open in new window

and this:
function chk_skip_summ() {
	if (nosumm == "1") {
		window.location = "send_email_ls250.php?ddocs=" + ddocs;
	}	
}

Open in new window


Of course the page it is on loads, then the page send_mail_ls250.php loads if nosumm is "1".

The customer does not like the first page flashing up, then disappearing.

Is there a way I can do this so the first page does not show AT ALL?

Thanks
Richard KortsAsked:
Who is Participating?
 
Dave BaldwinFixer of ProblemsCommented:
The method works but there may be something wrong with your logic.  Here's a simple test program you can play around with.  You might want to use 'display' instead of 'visibility'.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Onload Test</title>
<script type="text/javascript">
<!--
function makevisible() {
	document.getElementById("container1").style.visibility = 'visible';
	document.getElementById("container2").style.visibility = 'hidden';
	document.getElementById("container3").style.display = 'inline';
	document.getElementById("container4").style.display = 'none';
	}
// -->
</script>
</head>
<body onload="makevisible();">
<p>The first 'div' below should be visible after the page is loaded and the second 'div' should be hidden.</p>
<div id="container1"  style="visibility: hidden;">This is the first 'div'.</div>
<div id="container2"  style="visibility: visible;">This is the second 'div'.</div>
<p>The third 'div' below should be visible after the page is loaded and the fourth 'div' should be hidden.</p>
<div id="container3"  style="display: none;">This is the third 'div'.</div>
<div id="container4"  style="display: inline;">This is the fourth 'div'.</div>
</body>
</html>

Open in new window

0
 
zephyr_hex (Megan)DeveloperCommented:
Wrap all of your content in an element like a div, and set that div to invisible.  Then, if the page does not redirect, set it to visible.

Demo: https://jsfiddle.net/zephyr_hex/fwmsd83m/

<body onLoad="save_proj(); chk_ems(); set_pointers(); chk_skip_summ();">
   <div id="container" style="visibility: hidden;">
     //content here
   </div>
</body>

Open in new window


function chk_skip_summ() {
	if (nosumm == "1") {
		window.location = "send_email_ls250.php?ddocs=" + ddocs;
	}
        //show body
        document.getElementById("container").style.visibility = 'visible';
}

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
Something like Megan suggested would be necessary... because 'onload' means after the page has loaded.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Richard KortsAuthor Commented:
Does not seem to work.

Top <body> code now looks like this:
<body onLoad="chk_skip_summ(); save_proj(); chk_ems(); set_pointers(); chk_skip_summ();">
<div id="container" style="visibility: hidden;"> 

Open in new window


chk_skip_summ() looks like this:
function chk_skip_summ() {
	if (nosumm == "1") {
		window.location = "send_email_ls250.php?ddocs=" + ddocs;
	}	
	//show body
    document.getElementById("container").style.visibility = 'visible';
}

Open in new window


There is no other element on the page with id of container.

If you send me a private emai, I can email you a login & password & steps to reproduce.

Thanks

Richard
0
 
Richard KortsAuthor Commented:
Dave,

All that works fine.

It seems there is some problem with timing.  I changed the Javascript function to:
function chk_skip_summ() {
	alert("nosumm = " + nosumm);
}

Open in new window

Removing the window.location = "send_email_ls250.php?ddocs=" + ddocs;

The page is blank (after the alert is answered).

Then I changed the function to:

function chk_skip_summ() {
	alert("nosumm = " + nosumm);
        document.getElementById("container").style.visibility = 'visible';
}

Open in new window


Then the page displays after the alert is answered.

It looks like there is a pause before the window.location is executed.

I am thinking that the fact there are 4 functions referenced in the onLoad makes it execute the other 3 (with visibility on) BEFORE the window.location is executed.

Does that make sense?

Thanks,

Richard
0
 
Richard KortsAuthor Commented:
I got it to work.

I removed the function chk_skip_summ() from the onLoad action.

At the VERY end of the body, after the body tag, I put in this Javascript:

</body>
<script>
	if (nosumm == "1") {
		window.location = "send_email_ls250.php?ddocs=" + ddocs;
	} else {
		document.getElementById("container").style.visibility = 'visible';
	}	
</script>

Open in new window


It goes to the other php WITHOUT flashing the page image first.

Richard
1
 
Richard KortsAuthor Commented:
Thanks for pointing out the use of visible & hidden; I ultimately used that.

I still don;t see why the doesn't work as part of onLoad, but a solution that works is what I needed.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.