Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: cookie help

  1. #1
    mex
    mex is offline
    Regular Coder
    Join Date
    Jul 2003
    Location
    somewhere
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    cookie help

    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.

    <html><head><title>mex</title>
    <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;
    //-->
    </script>
    </HEAD>

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

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    If you do that, an error would occur because the span with the id welcome is not yet loaded.

  • #3
    mex
    mex is offline
    Regular Coder
    Join Date
    Jul 2003
    Location
    somewhere
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    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
    Wonder

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,965
    Thanks
    0
    Thanked 236 Times in 233 Posts
    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.

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

  • #5
    mex
    mex is offline
    Regular Coder
    Join Date
    Jul 2003
    Location
    somewhere
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanx for the help......
    Wonder


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •