jQuery validation to allow only numbers OR only characters

Allow only numbers 
$(‘#zip’).keypress(function(event){

if(event.which != 8 && isNaN(String.fromCharCode(event.which))){

event.preventDefault();

}
});

Allow Characters with dots
$(‘#name’).keypress(function(event){

var inputValue = event.charCode;  if(!(inputValue >= 65 && inputValue <= 120) && (inputValue != 32 && inputValue != 0  && inputValue != 46)){

event.preventDefault();

}

});

Advertisements

Browser close & reload events in jQuery

/*Browser close jQuery event*/
Put this code body tag –> onunload=”doUnload()”

Below code for script
function doUnload()
{
if(window.event.clientX < 0 && window.event.clientY <0){
var cookieName = ‘suitebeb123’;
$.removeCookie(cookieName);
}
}

/*Broser reload javascript function*/
window.onbeforeunload = function (e) {
// Your logic to prepare for ‘Stay on this Page’ goes here
}

Smooth Scroll using jQuery

Please find below code for smooth scroll

$(document).ready(function(){
$(“a”).on(‘click’, function(event) {
if (this.hash !== “”) {
event.preventDefault();
var hash = this.hash;
$(‘html, body’).animate({
scrollTop: $(hash).offset().top
}, 1800, function(){
window.location.hash = hash;
});
}
});
});

window.location in javascript

Please find below code…

//Get current page url
var curr_url = window.location.href

//Get hostname
var host_name = window.location.hostname

//Display the path name of the current URL
var path_name = window.location.pathname

//Get protocol of URL return http:// or https://
var proto = window.location.protocol;

//Get parameters from URL
var x = location.search;
Result:
?email=someone@example.com