var e_slideIn = document.getElementById('slideIn');
var e_slideInShade = document.getElementById('slideInShade');
function f_slideIn() {
if (!window.e_slideIn) return;
var n_width = e_slideIn.offsetWidth;
var n_height = e_slideIn.offsetHeight;
var n_left = (f_clientWidth() - n_width) / 2;
var n_top = parseInt(e_slideIn.style.top);
var n_moveTo = (f_clientHeight() - n_height) / 2;
You need to set a cookie. Many examples have been posted in this forum - here is one of them. You set the expiry date to the desired number of days ahead (here 365 days - change yours to 1 or whatever).
Code:
<html>
<head>
<title>Welcome Message Displays Once Only</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
</script>
</head>
<body>
<script type="text/javascript">
if(!readCookie('visitedPreviously')){
document.write(' Your Message Goes Here And You See It Only Once ');
createCookie('visitedPreviously', 'visitedPreviously', 365); // 365 days persistence
}
//eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
</script>
</body>
</html>
Note that "a day" means 24 hours, not exactly the same thing as the next day starting at midnight.
Quizmaster: What is the only prime number between 75 and 80?
Contestant: 99
What is wrong with the code I gave you in Post #2? And why are you shouting?
Presumably you would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked.
What is wrong with the code I gave you in Post #2? And why are you shouting?
Presumably you would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked.
Please help. I need the code put in my code somehow. I don't know how to put JavaScript into a JavaScript; my code is a slide-in ad & I don't know how to make it only show once to a visitor per session. I'd also like to know how to do it per day.
I've tried codes like yours I can't figure it out & need help getting it working with my code specifically..
a) delete this line f_slideIn(); which you will find right in front of </script>
b) place the two scripts which I gave you in Post#2 in the <head> and <body> respectively of your page. Right after the <head> and the <body> tags.
c) change the script in the body as follows:-
Code:
<body>
<script type="text/javascript">
if (!readCookie('visitedPreviously')) {
//document.write(' Your Message Goes Here And You See It Only Once '); // comment this line out but you may care to retain it for test purposes
createCookie('visitedPreviously', 'visitedPreviously', 1); // 1 days persistence
f_slideIn(); // call the function
}
Help; this doesn't work. The code above doesn't work with my code. Here is what I did to combine my code with the above code & it does NOT work
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html> <head> <title>Untitled</title>
<script type="text/javascript"> function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }
function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; }
function eraseCookie(name) { createCookie(name,"",-1); }
</script>
</head>
<body>
<script type="text/javascript"> if(!readCookie('visitedPreviously')){ document.write(' <div id="slideIn" style="position:absolute; width: 720px; height: 320px; left: 0; top: -600px; z-index: 100; border: 1px solid gray; padding: 20px; font-family: 14px Verdana, sans-serif, Tahoma, Arial; font-color: black;" <div style="text-align:right"><input type="button" value="CLOSE" onclick="f_slideOut()" style="background: black; color: white; border: 2px solid #dcdcdc;"></div> <span style="font-family: Verdana, sans-serif, Tahoma, Arial; font-size: 13px; text-align: center;"> <b>Buying/Selling/Trading MMORPG Accounts?</b> <br /><br /> <b>YOU HAVE A <u>70% PROBABILITY OF BEING SCAMMED</u> OUT OF YOUR MMORPG ACCOUNT AND/OR MONEY.</b> Whether you're a MMORPG account buyer, seller, and/or trader, the odds are against you; MMORPG-related transactions statistically hold a 70% fraud probability. Since 2006, EGS has served many thousands of MMORPG gamers overcome fraud. <b>Our <a href="http://www.egamingsupply.com/middleman-service-order-page" title="Middleman Service" style="color: #336699; text-decoration: underline;">Middleman Service</a> provides 99.9% safer transactions.</b> <i>SPECIAL: Save 20%! Use coupon code "<u>Monarch20Bday</u>" at check-out.</i> <a href="http://www.egs.us/buy/middleman-service" title="EGS Middleman Service"><img src="images/egsldr3.gif" style="border: none;" alt="EGS Middleman Service" /></a> </div> <div id="slideInShade" style="position:absolute;z-index:99;visibility:hidden;"></div> '); createCookie('visitedPreviously', 'visitedPreviously', 1); // 365 days persistence }
eraseCookie('visitedPreviously'); // FOR TEST PURPOSES
var e_slideIn = document.getElementById('slideIn'); var e_slideInShade = document.getElementById('slideInShade');
function f_slideIn() { if (!window.e_slideIn) return; var n_width = e_slideIn.offsetWidth; var n_height = e_slideIn.offsetHeight; var n_left = (f_clientWidth() - n_width) / 2; var n_top = parseInt(e_slideIn.style.top); var n_moveTo = (f_clientHeight() - n_height) / 2;
a) delete this line f_slideIn(); which you will find right in front of </script>
b) place the two scripts which I gave you in Post#2 in the <head> and <body> respectively of your page. Right after the <head> and the <body> tags.
c) change the script in the body as follows:-
Code:
<body>
<script type="text/javascript">
if (!readCookie('visitedPreviously')) {
//document.write(' Your Message Goes Here And You See It Only Once '); // comment this line out but you may care to retain it for test purposes
createCookie('visitedPreviously', 'visitedPreviously', 1); // 1 days persistence
f_slideIn(); // call the function
}
wtf? The whole point is to combine two scripts. I want teh slide in functino this is a slide in window that I want to show only once per day/once per session.
Sorry, you have not done what I suggested, and your boorish ill-manners means that I am not interested in helping you further. I don't like it when people shout at me.
In any case you are obviously too inexperienced and lacking in knowledge of JavaScript to make use of any advice given.
Hi Philip M, wanted to express my gratitude for posting this answer and greatly helped me to achieve what i was looking for even if this a 9 months old thread. i am an inexperienced coder my self (not even a coder) i am a web designer but polite and have always learned to give credit to people like you , sharing knowledge is what is driving all of us to go forward..
i got here via google and this was exactly what i was looking for just i want this to modify it to accept lesser period than a day eg 1 hour.
you can see the use of it on this website www.myfxcentral.gr it uses your code to show the jpopup in joomla only once per user per day, but i want this to set it a lesser period.
thank you.
john
btw if you need anything to see how i used your code or anything i will provide asap thanks
Last edited by kegra; 06-18-2011 at 05:41 PM..
Reason: more info
Please help.
I don't know how to set a cookie with my particular code. Can someone help me & provide it in my code and paste it here? I have no idea what to do.
Here's a set of functions that will save or retrieve data from a cookie or from local storage:
Code for cookies:
Code:
/***
* write a cookie: name, value, lifetime in days
**/
var writeCookie = function (name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 86400000));
var expires = '; expires=' + date.toGMTString();
} else {
expires = '';
}
var content = name + '=' + value + expires;
document.cookie = content;
return content;
}
/***
* read a cookie: name. returns value or false if not found
**/
var readCookie = function (name) {
var nameEQ = name + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
}
return false;
}
Example for a cookie:
(1) var name = 'Joe Smith';
(2) writeCookie('username', name, 30); (a cookie named 'username' with a value of 'Joe Smith' is saved on the client's machine and it stays there for 30 days.
(3) .... sometime later.....
(4) var name = readCookie('username');
(5) alert(name); (displays 'Joe Smith' retrieved from the client's machine).
Code for local storage:
Code:
/***
* read data from local storage, return false if not there
***/
var getLocal = function (name) {
return localStorage.getItem(name) || false;
}
/***
* save data to local storage, return false if failed
***/
var setLocal = function (name, value) {
localStorage.setItem(name, value);
if (getLocal(name) !== value) {
return false;
}
return value;
}
/***
* clear one entry from local storage
***/
var clrLocal = function (name) {
return localStorage.removeItem(name);
}
/***
* completely wipe all local storage clean
***/
var nukeLocal = function () {
var n = localStorage.length;
if (n) {
while (n--) {
localStorage.removeItem(localStorage[n]);
}
}
}
Note that you cannot store OBJECTS in a cookie or in local storage. You must first use JSON.stringifier (to convert an object into a storable form) and JSON.parse (to convert a cookie or local storage string back into an object).
Hope this helps you.......
-- Roger
__________________ "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov
If you have a server side language you can use I would recommend doing this server side as any cookies and/or javascript solutions will not work if they are disabled in the visitors' browser.
If you have a server side language you can use I would recommend doing this server side as any cookies and/or javascript solutions will not work if they are disabled in the visitors' browser.
You keep making this infantile observation. Only a tiny minority of people disable Javascript in their browser - and if they do they do it deliberately and at their own risk. And if Javascript is disabled than pretty obviously kegra's site (reliant on many scripts including jQuery and mootools!) will not work at all.
Disabled cookies is a more plausable situation, but in that case the cookies are disabled both from client-side and from server-side.
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.