Reputation: 9
i have code to check for browser and at the moment it is running each time i refresh page. is it possible to run only once on each visit so if i refresh or are taken to that page by link then the code is ignored. thanks
<script type="text/javascript">
if($.browser.msie && $.browser.version=="8.0" || $.browser.msie && $.browser.version=="7.0")
alert("Please upgrade your browser to at least version 8.0 in order to use all the features of this site.\n\nThank you.");
</script>
updated code supplied by darin:
<script type="text/javascript">
var date = new Date(); date.setTime(date.getTime() + (30 * 60 * 1000)); $.cookie("explorer", "foo",{ expires: date });
var cookie = $.cookie('explorer');
if (cookie == null) {
// the cookie is not present => you may do the checks
if ($.browser.msie && $.browser.version == '8.0') {
// set the cookie so that the next time the user visits this page
// he doesn't get the alert message
$.cookie('explorer', 'true');
alert("Please upgrade your browser to at least version 8.0 in order to use all the features of this site.\n\nThank you.");
}
}
</script>
the alert is not firing though
Upvotes: 1
Views: 743
Reputation: 1074168
You'll have to set a cookie remembering what the last version you saw was and only nag the user if you don't see the cookie or if the version changes but still isn't what you want.
Side note: Your check, as quoted, will nag someone using IE8 that they need to at least use IE8. Seems odd. If you really want browser sniffing, perhaps:
if ($.browser.msie && parseFloat($.browser.version) < 8) {
// Do whatever you're going to do with them having IE < 8.0
}
Off-topic: This sort of nagging pop-up is out of fashion. Instead, if there are parts of your site that won't work without certain browser features, use feature detection (rather than browser sniffing) to check for them and disable only the relevant parts (perhaps with a message, when the user tries to use that part of your site, saying why they can't). You can find a bunch of useful feature tests here and here. See also jQuery.support
.
Upvotes: 3
Reputation: 1038730
You could use cookies. So you may check for the presence of some custom cookie and if not present show the message and emit the cookie so that the next time the message is not shown. Here's a nice plugin for jquery that simplifies reading and writing cookies:
<script type="text/javascript">
var cookie = $.cookie('some_cookie_name');
if (cookie == null) {
// the cookie is not present => you may do the checks
if ($.browser.msie && $.browser.version == '7.0') {
// set the cookie so that the next time the user visits this page
// he doesn't get the alert message
$.cookie('some_cookie_name', 'true');
alert("Please upgrade your browser to at least version 8.0 in order to use all the features of this site.\n\nThank you.");
}
}
</script>
Upvotes: 1