Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joe-dempsey/140e4a5b81d7a22a25be0e396ef481ce to your computer and use it in GitHub Desktop.
Save joe-dempsey/140e4a5b81d7a22a25be0e396ef481ce to your computer and use it in GitHub Desktop.
Geo popup example.
<script type="text/javascript">
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
// If the visitor is browsing from US.
if (location.country_code === 'US') {
// DO NOTHING for now
} else {
//getcookie bs
function getCookie(cname) {
var name = cname + "=";
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);
if(c.indexOf(name) == 0)
return c.substring(name.length,c.length);
}
return "";
}
function getNewUrl(){
if(hostname.indexOf('www.') === 0){
var new_url = protocal+'//'+hostname.replace('.us','')+pathname;
return new_url;
} else {
var new_url = protocal+'//'+hostname.replace('.us','')+pathname;
return new_url;
}
}
// Get hostname and pathname
var current_url = window.top.location.href;
var hostname = window.top.location.hostname;
var pathname = window.top.location.pathname;
var protocal = window.top.location.protocol;
var seenmodal = getCookie("seenmodal");
if (seenmodal == "TRUE") {
// don't load the popup - AS THEY'VE ALREADY CLOSED the modal
} else {
// show popup
{% comment %} pop block {% endcomment %}
var new_location = getNewUrl();
var modal = document.getElementById('geoModal');
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var close_text = document.getElementsByClassName("close_text")[0];
// open the modal on load
document.onload = setTimeout(function() {
modal.style.display = "block";
}, 500);
document.getElementById("modal_btn").href = current_url.replace('us.', '');
// When the user clicks on <span> (x), close the modal and set cookie
span.onclick = function() {
modal.style.display = "none";
document.cookie = "seenmodal=TRUE; path=/";
}
// close text - display none and set cookie
close_text.onclick = function() {
modal.style.display = "none";
document.cookie = "seenmodal=TRUE; path=/";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
document.cookie = "seenmodal=TRUE; path=/";
}
}
{% comment %} end pop block {% endcomment %}
}
}
}
} );
</script>
<!-- The Modal -->
<div id="geoModal" class="modal-new" style="display:none;">
<!-- Modal content -->
<div class="modal-content center">
<span class="close close_styles">&times;</span>
<div class="center"><img src="{{ 'logo.png' | asset_url }}" style="width:180px;margin-top:20px;" ></div>
<h2 class="modal_heading">{{ settings.popup_line_1 }}</h2>
<p class="modal_p">{{ settings.popup_line_2 }}</p>
<p class="modal_p">{{ settings.popup_line_3 }}</p>
<div class="center">
<a class="btn modal_special" id ="modal_btn" href="https://us.urbanexcess.com/" >{{ settings.popup_line_4 }}</a>
<span class="close_text"><p class="modal_p_final">{{ settings.popup_line_5 }}</p></span>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment