View Full Version : cookie help

02-10-2004, 02:13 PM
This script is working but it loads only after the whole page is loaded. Can anyone modify this script in such a way that it prompts before loading the site.

<script type="text/javascript">

function doWelcome() {
var dc = document.cookie;
if(dc.length > 0) {
document.getElementById('welcome').innerHTML = 'Welcome back '+ dc;

if(dc.length == 0) {
var yourname = prompt('Do enter your name for a proper welcome :', '');
if (yourname == '' || yourname == 'null') {yourname = 'mex';}
else {yourname = yourname;}
document.getElementById('welcome').innerHTML = 'Welcome '+ yourname;
var today = new Date();
today.setTime(today.getTime() + 1*60*60*1000);
var expString = "; expires=" + today.toGMTString();
document.cookie = yourname + expString;

window.onload = doWelcome;

<span id="welcome"></span> </body></html>

02-11-2004, 06:56 AM
If you do that, an error would occur because the span with the id welcome is not yet loaded.

02-11-2004, 10:59 AM
no it works fine.....What i realy meant to say was: other codes, pictures and others are loaded first and when everyting is done, the script pops up asking for your name!.......

The want the script to be modified in such a way that it ask for your name before anything is loaded in the site.

Hope this willhelp u understand. Thanx for the reply

02-12-2004, 01:45 AM
I understood you right in your first post. What I meant was, if you call doWelcome() (in the head) before the <span> tag is rendered, there would be an error because inside the function, the text entered by the user is inserted inside the span that's not yet loaded.

Here's what you should do. Remove the onload handler and call the function after the span tag.

<span id="welcome"></span>
<script type="text/javascript">
doWelcome(); //call prompt, span already loaded
<!--other contents here-->

02-16-2004, 04:00 AM
thanx for the help......