I have an open div on my site, a welcome message, which has a slideUp/slideDown toggle, so the user can click 'Close' and it slides up the div. However, everytime you land on this page, it's open... and I'd like to apply a cookie to this so if the user presses the slideUp toggle, it adds a cookie and keeps it at display:none.
Here is my jQuery code so far... and I'm hoping to use the cookie plugin which might make things easier?
var welcome_active = false;
$('.welcome-container .trigger span').click(function() {
if (!welcome_active) {
$('.welcome-container .content').slideUp('slow', function() {});
$(this).find('span').removeClass('ss-navigatedown');
$(this).find('em').html('About');
$(this).find('span').addClass('ss-navigateup');
welcome_active = true;
console.log('test');
} else {
$('.welcome-container .content').slideDown('slow', function() {});
$(this).find('span').removeClass('ss-navigateup');
$(this).find('em').html('Close');
$(this).find('span').addClass('ss-navigatedown');
welcome_active = false;
}
});
Any ideas if this is possible?
Thanks
Should be something like this, using the cookie plugin you mentioned, but you can also use local storage if you don't have to support older browsers:
var welcome_active = false;
if($.cookie('welcome_active')=='1'){
$('.welcome-container .content').slideUp('slow', function() {});
$(this).find('span').removeClass('ss-navigatedown');
$(this).find('em').html('About');
$(this).find('span').addClass('ss-navigateup');
welcome_active = true;
}
$('.welcome-container .trigger span').click(function() {
if (!welcome_active) {
$('.welcome-container .content').slideUp('slow', function() {});
$(this).find('span').removeClass('ss-navigatedown');
$(this).find('em').html('About');
$(this).find('span').addClass('ss-navigateup');
welcome_active = true;
console.log('test');
$.cookie('welcome_active', '1', { expires: 7 });
} else {
$('.welcome-container .content').slideDown('slow', function() {});
$(this).find('span').removeClass('ss-navigateup');
$(this).find('em').html('Close');
$(this).find('span').addClass('ss-navigatedown');
welcome_active = false;
$.cookie('welcome_active', '0', { expires: 7 });
}
});
You can add a cookie simply by editing document.cookie
ex :
document.cookie="slideState=up";
Next you just have to read document.cookie and parse the string to get your value.
I suggest you to use localstorage it's simple and efficient and it has a very good compatibility
Related
I'm using AngularJs version 1.4, I need to detect when user is not on the tab of the my app and when he comes back so I tried using $watch this way:
$rootScope.$watch(angular.bind($window, function(document){
return this.document.visibilityState
}), function (n, o) {
if (n === 'visible' && o == 'hidden') {
console.log('Im back')
}
})
The problem that when I go back to the tab it takes a lot of time until it activates the response function on change, something like 5 seconds.
Anyone has any idea why?
You can try onfocus and onblur of window :
window.onblur = function() { console.log('blur'); }
window.onfocus = function() { console.log('focus'); }
It works with simple java script.
In angular you can try this
$window.onblur = function() { console.log('blur'); }
$window.onfocus = function() { console.log('focus'); }
I'm trying to set an element so it's being shown only once per visit. It's a scroll down arrow on my homepage and so once the user gets it it won't be necessary to keep it anymore. So I don't want it to be shown while the user is surfing on my website however, when he visits it again in the future it's there again. I'm a newbie and can't quite solve it.
My code:
setTimeout(function () {
$('.scroll_down').show()
}, 2000);
var $element = $('.scroll_down'); // fade out / in on scroll
$(window).scroll(function() {
if($(this).scrollTop() > 0) {
$element.fadeOut(1000);
}
});
I also would like the arrow to fade in but my attempts were not successful. Thanks guys
Please write cookie code as follow:
jQuery(document).ready(function($){
if($.cookie('show_div_once') != 'yes'){
your_code_for_show_div;
}
$.cookie('show_div_once', 'yes', { path: '/', expires: 365 });
});
I used localStorage
firstSiteLoad = (function() {
var checkSupport;
checkSupport = function() {
var e, error, support;
try {
support = 'localStorage' in window && (window['localStorage'] != null);
} catch (error) {
e = error;
support = false;
}
return support;
};
return function() {
if (!checkSupport()) {
return false;
}
if (localStorage.getItem("not_first_load")) {
return false;
} else {
localStorage.setItem("not_first_load", 'true');
return true;
}
};
})();
you can use it by if (firstSiteLoad()) { //your code }
I am using this script from: http://pop.seaofclouds.com/
The problem is if you call the script multiple times it causes a cascading effect of a pop-out within a pop-out for as many times as you call the script.
I'm trying to figure out how to prevent it from executing when the popout has already been set. Here's the script:
//
// pop! for jQuery
// v0.2 requires jQuery v1.2 or later
//
// Licensed under the MIT:
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright 2007,2008 SEAOFCLOUDS [http://seaofclouds.com]
//
(function($) {
$.pop = function(options){
// inject html wrapper
function initpops (){
$(".pop").each(function() {
var pop_classes = $(this).attr("class");
if ( $(this).find('.pop_menu').length) {
// do nothing
} else {
$(this).addClass("pop_menu");
$(this).wrap("<div class='"+pop_classes+"'></div>");
$(".pop_menu").attr("class", "pop_menu");
$(this).before(" \
<div class='pop_toggle'></div> \
");
}
});
}
initpops();
// assign reverse z-indexes to each pop
var totalpops = $(".pop").length + 100;
$(".pop").each(function(i) {
var popzindex = totalpops - i;
$(this).css({ zIndex: popzindex });
});
// close pops if user clicks outside of pop
activePop = null;
function closeInactivePop() {
$(".pop").each(function (i) {
if ($(this).hasClass('active') && i!=activePop) {
$(this).removeClass('active');
}
});
return false;
}
$(".pop").mouseover(function() { activePop = $(".pop").index(this); });
$(".pop").mouseout(function() { activePop = null; });
$("body").on("click", ".pop", function(){
closeInactivePop();
});
// toggle that pop
$("body").on("click", ".pop_toggle", function(){
$(this).parent(".pop").toggleClass("active");
});
}
})(jQuery);
now when i load this script on an ajax call the new pop-out menus work but the old ones do not react to the onclick event.
You shouldn't mess with the plugin. It works exactly like it should.
Better show us how you call this on elements that you already have.
Also I don't like this plugin. Better use something from JqueryUI
You can do such thing in much easier way.
[edit]
I tried your first code (the plugin) and it works correctly for me.
[edit]
OK. I get it. You call $.pop(); multiple times. You shouldn't! Calling $.pop(); will pin up the drop down menu to all elements that has class="pop". This is the reason why you have such funny stack.
Just use $.pop(); once.
Plugin doesn't give ability to connect NEW elements that was dynamically created on the page.
Removed pop from ajax call and just called this on success:
$(".pop").each(function() {
var pop_classes = $(this).attr("class");
if ( $(this).find('.pop_menu').length) {
// do nothing
} else {
$(this).addClass("pop_menu");
$(this).wrap("<div class='"+pop_classes+"'></div>");
$(".pop_menu").attr("class", "pop_menu");
$(this).before(" \
<div class='pop_toggle'></div> \
");
}
});
// assign reverse z-indexes to each pop
var totalpops = $(".pop").length + 100;
$(".pop").each(function(i) {
var popzindex = totalpops - i;
$(this).css({ zIndex: popzindex });
});
// close pops if user clicks outside of pop
activePop = null;
function closeInactivePop() {
$(".pop").each(function (i) {
if ($(this).hasClass('active') && i!=activePop) {
$(this).removeClass('active');
}
});
return false;
}
$(".pop").mouseover(function() { activePop = $(".pop").index(this); });
$(".pop").mouseout(function() { activePop = null; });
I heard that Opera doesn't work with beforeunload. And some earlier versions of IE also. So how can I use these together ? If I use below, it runs 2 times (They all work) in Firefox.
$(window).bind('beforeunload', function () {
});
$(window).unload(function () {
});
There are several ways of doing this. Following example is quite straight forward using a global flag.
var hasUnloadBeenHandled = false;
function onUnload() {
if (hasUnloadBeenHandled) {
return;
}
//Whatever you want to be handled on unload OR on beforeunload
hasUnloadBeenHandled = true;
}
$(window).bind('beforeunload', function () {
onUnload();
});
$(window).unload(function () {
onUnload();
});
you need to use a flag of sort or you can use something like this http://documentcloud.github.com/underscore/#once
function unloadHandler(m){
alert(m);
}
if ($(window).unload) {
$(window).unload(function() {
unloadHandler("unload");
});
} else if ($(window).beforeunload) {
$(window).bind('beforeunload', function() {
unloadHandler("beforeunload");
});
}
Im not a Javascript/JQuery guy, but cant you just use a global variable to keep track of whether or not the method should run?
something like
var runme = true;
$(window).bind('beforeunload', function () {
if(runme){
runme=false;
}
});
$(window).unload(function () {
if(runme){
runme=false;
}
});
Context: On my product website I have a link for a Java webstart application (in several locations).
My goal: prevent users from double-clicking, i. e. only "fire" on first click, wait 3 secs before enabling the link again. On clicking, change the link image to something that signifies that the application is launching.
My solution works, except the image doesn't update reliably after clicking. The commented out debug output gives me the right content and the mouseover callbacks work correctly, too.
See it running here: http://www.auctober.de/beta/ (click the Button "jetzt starten").
BTW: if anybody has a better way of calling a function with a delay than that dummy-animate, let me know.
JavaScript:
<script type="text/javascript">
<!--
allowClick = true;
linkElements = "a[href='http://www.auctober.de/beta/?startjnlp=true&rand=1249026819']";
$(document).ready(function() {
$('#jnlpLink').mouseover(function() {
if ( allowClick ) {
setImage('images/jetzt_starten2.gif');
}
});
$('#jnlpLink').mouseout(function() {
if ( allowClick ) {
setImage('images/jetzt_starten.gif');
}
});
$(linkElements).click(function(evt) {
if ( ! allowClick ) {
evt.preventDefault();
}
else {
setAllowClick(false);
var altContent = $('#jnlpLink').attr('altContent');
var oldContent = $('#launchImg').attr('src');
setImage(altContent);
$(this).animate({opacity: 1.0}, 3000, "", function() {
setAllowClick(true);
setImage(oldContent);
});
}
});
});
function setAllowClick(flag) {
allowClick = flag;
}
function setImage(imgSrc) {
//$('#debug').html("img:"+imgSrc);
$('#launchImg').attr('src', imgSrc);
}
//-->
</script>
A delay can be achieved with the setTimeout function
setTimeout(function() { alert('something')}, 3000);//3 secs
And for your src problem, try:
$('#launchImg')[0].src = imgSrc;
Check out the BlockUI plug-in. Sounds like it could be what you're looking for.
You'll find a nice demo here.
...or just use:
$(this).animate({opacity: '1'}, 1000);
wherever you want in your code, where $(this) is something that is already at opacity=1...which means everything seemingly pauses for one second. I use this all the time.
Add this variable at the top of your script:
var timer;
Implement this function:
function setFlagAndImage(flag) {
setAllowClick(flag);
setImage();
}
And then replace the dummy animation with:
timer = window.setTimeout(function() { setFlagAndImage(true); }, 3000);
If something else then happens and you want to stop the timer, you can just call:
window.clearTimeout(timer);