Results 1 to 5 of 5

Thread: popup div only once

  1. #1
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default popup div only once

    Any ideas why this doesnt work? I dont see my popup at all. Im trying to make my div appear only the first time the user goes to the page...

    <script language="JavaScript">
    var isloaded=0
    window.onload=function(){
    if (document.cookie.indexOf("isloaded")==-1){
    document.getElementById("whyWeAsk").className == 'popperHid' ? document.getElementById("whyWeAsk").className = 'popperShow' : document.getElementById("whyWeAsk").className = 'popperHid';
    return false;
    document.cookie="isloaded=1"
    }
    }
    </script>

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    I'll need to see the classes (in your css) to see what you're doing.

    You're using "popperHid" and "popperShow"

    I'm guessing "popperHid" is to hide the div, and the other is to display it. But until I have a definite answer, I can't help you
    - Mike

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ldoozer
    Any ideas why this doesnt work?
    Nothing definitive without seeing an example. There might be an error elsewhere that prevents execution of the code. The body element might have an onload attribute. Please post a link to a demonstration. It doesn't have to be the document you're working on, just an example of what you're trying to do and the code you're trying to use to do it.

    In the meantime:

    <script language="JavaScript">
    The language attribute has been deprecated for a long time. Use the type attribute:

    HTML Code:
    <script type="text/javascript">
    var isloaded=0
    This variable isn't used, at least in the code you posted. You should consider removing it.

    if (document.cookie.indexOf("isloaded")==-1){
    Before using that the cookie property, you should check to make sure it exists, is a string, and actually behaves as a dynamic property. If cookies are disabled, none of these things may be true, and your code will raise an exception.

    Whilst on the subject, have you considered the impact that your scheme will have on users with cookies disabled? Will it be very intrusive if every time they return, this pop-up of yours will be shown?

    document.getElementById("whyWeAsk").className == 'popperHid' ? document.getElementById("whyWeAsk").className = 'popperShow' : document.getElementById("whyWeAsk").className = 'popperHid';
    Yikes! What on Earth is that? You seem to have confused when to use a conditional statement, and the conditional operator (?:&#41;. Either use:

    Code:
    /* Check for support of the getElementById method before using it. */
    if (document.getElementById) {
        /* Save a reference to the element; there's no need to obtain it multiple times. */
        var popUp = document.getElementById("whyWeAsk");
    
        if (popUp)
            if (popUp.className == "popperHid") popUp.className = "popperShow";
            else popUp.className = "popperHid";
    }
    or:

    Code:
    if (document.getElementById) {
        var popUp = document.getElementById("whyWeAsk");
    
        if (popUp) popUp.className = (popUp.className == "popperHid")
                ? "popperShow" : "popperHid";
    }
    The conditional operator should only be used within expressions, just like any other operator. It is not a substitute for an if statement.

    return false;
    Surely that's a mistake?

    document.cookie="isloaded=1"
    This won't ever be executed due to the return statement above, but even if it was, are you aware that it will only create a session cookie? The next time the user loads their browser and returns to your site, the cookie will have expired and the pop-up will appear again. Is that your intention?

    Mike

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    The language attribute has been deprecated for a long time. Use the type attribute
    Really? I had no idea
    - Mike

  5. #5
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by mburt
    Really? I had no idea
    Yep. Since the first working draft of HTML 4.0, published on 7th July 1997. That's just over nine years ago. As I said, it's been deprecated for a long time.

    Mike

Bookmarks

Posting Permissions

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