Last active
February 19, 2019 17:04
-
-
Save djrmom/470fb23f3db8cc8d3a2606cfda2795a2 to your computer and use it in GitHub Desktop.
facetwp modified "How to Preserve Facet Selections Across Pages" code for beaver builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
(function($) { | |
$(function() { | |
/* | |
After FacetWP reloads, store any updates into a cookie | |
*/ | |
$(document).on('facetwp-loaded', function() { | |
if ( typeof FLBuilder == 'undefined' ) { | |
var date = new Date(); | |
var facets = window.location.search; | |
date.setTime(date.getTime()+(24*60*60*1000)); | |
document.cookie = "facetdata="+facets+"; expires="+date.toGMTString()+"; path=/"; | |
} | |
}); | |
/* | |
When FacetWP first initializes, look for the "facetdata" cookie | |
If it exists, set window.location.search= facetdata | |
*/ | |
$(document).on('facetwp-refresh', function() { | |
if (! FWP.loaded && typeof FLBuilder == 'undefined' ) { | |
var facets = window.location.search; | |
var facetdata = readCookie('facetdata'); | |
if (null != facetdata && '' != facetdata && facets != facetdata) { | |
document.cookie = 'facetdata=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/'; | |
window.location.search = facetdata; | |
} | |
} | |
}); | |
/* | |
Cookie handler | |
*/ | |
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; | |
} | |
}); | |
})(jQuery); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment