Its been a while since I've been here. I need some help. I am in need of a JS code which can set a cookie for a domain so that its active while a user is visiting the site and its pages, but upon leaving the site by any method (ie closing the window or leaving the domain) an alert box with a message appears. The alert box should only popup once, when they leave, not when they go to each page of the site. I am a designer so my basic knowledge of JS won't do in this instance. IF anyone can please help I'd be a happy designer.
Thanks and happy coding.
~Metallifan15 :cool:
12-01-2011, 04:12 PM
JunkMale
This would require click monitoring, sites within your domain do not trigger the event whereas clicking on a domain that is external would result in the pop up...
So
Code:
function processThisClick(cl){
yourLoc = window.location.hostname; // gets www.domain.com for this page
if( yourLoc != cl.href ){ // cl.href would need to be stripped to its domain.
// this bit runs because the link domain is external to the current web page domain.
writeCookie("somecookiename",somedata); // a cookie setting function
alert("Your leaving!"); // pop up a message
}
12-01-2011, 04:25 PM
metallifan15
That looks easy enough JunkMale. Do I need to worry about what goes into the cookie, or just let it be?
Also, what changes do I have to make to which lines of the code?
Please and thanks!
~Metallifan15
12-01-2011, 05:42 PM
JunkMale
Well its an idea plucked out my head to point you in a direction. I have no idea about your code as you never posted any!
As for what I posted, again, idea, not testes and I don't expect that particular function to work as it IMHO needs more to it.
My best advice is to sit down, write out the easy bits so you got something to work with and then look at the tough bits as smaller jobs. Breaking a program in to elements or lots of smaller functions to ease your coding will help you develop you script quicker than sitting and banging your head with trying to figure out how something is done.
Its far better you expend your time and energies on breaking the job down and building the stuff you know then deal with the other later.
12-02-2011, 09:15 AM
metallifan15
Code Snippet
Code:
<script type='text/javascript'>
(function()
{
if( !/(^|\s|;)seenAlert=yes(;|\s|$)/.test( document.cookie ) )
{
if( !alert('"The Phrase That Pays" Call us and receive the phrase that pays to get $200 off our best price on any new or pre-owned vehicle in stock or a $100 gas card with any purchase 507-732-5127 Only valid for 48 hours after you receive the secret phrase.') )
{
history.back();
location.href = document.referrer;
}
else
document.cookie="seenAlert=yes";
}
})();
</script>
~Metallifan15 :cool:
This is a code I found on another thread here. Perhaps just some help modifying this to fit the above needs is acceptable . Again, I'm a designer not a developer so JavaScript is not my strength. I can understand how this basic code works, but I would like it to appear as mentioned initially, that the alert box appears when the viewer leaves the domain OR closes the window. And only appears once per session. So when the browser is closed the cookie is removed.
I would kindly like some help getting code to open an alert box when they close or leave a certain domain. The following script is from the above mentioned site.
Code:
<script type="text/javascript">
//Get cookie routine by Shelley Powers
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
// if cookie exists
if (offset != -1) {
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
function setcolor(what){
document.body.style.backgroundColor=what
document.cookie="bgcolor="+what
}
if (get_cookie("bgcolor")!="")
document.body.style.backgroundColor=get_cookie("bgcolor")
</script>
Is there a way to modify this section
Code:
function setcolor(what){
document.body.style.backgroundColor=what
document.cookie="bgcolor="+what
}
if (get_cookie("bgcolor")!="")
document.body.style.backgroundColor=get_cookie("bgcolor")
so that it calls up an alert box?
Please help. I'm not a JS expert I only have basic knowledge and right now I do not have resources or time to fully "learn" the expansive amount of JS knowledge to create this code myself.