PDA

View Full Version : popup div only once



ldoozer
08-02-2006, 10:14 AM
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>

mburt
08-02-2006, 01:48 PM
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 :)

mwinter
08-02-2006, 03:26 PM
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:



<script type="text/javascript">


var isloaded=0This 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:



/* 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:



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

mburt
08-02-2006, 08:51 PM
The language attribute has been deprecated for a long time. Use the type attribute

Really? I had no idea :)

mwinter
08-02-2006, 09:36 PM
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