Skip to content

Instantly share code, notes, and snippets.

@softsolution
Created April 30, 2016 04:20
Show Gist options
  • Save softsolution/537e745a4c20fca92a08e7744ef2e464 to your computer and use it in GitHub Desktop.
Save softsolution/537e745a4c20fca92a08e7744ef2e464 to your computer and use it in GitHub Desktop.
Push state function - изменение урла при ajax запросе
function pushState(){
//1 рабочий вариант
// var form = $('#products_filter');
// var params = form.serialize();
// var url = document.products_filter.action+'?'+params;
// if(url != window.location){
// window.history.pushState(null, null, url);
// }
//2 рабочий вариант
var params = serializeFilter();
var url = document.products_filter.action+'?'+params;
if(url != window.location){
window.history.pushState(null, null, url);
}
}
function serializeFilter (){
var fields = document.products_filter.elements;
var field, name, value, type;
var res = '';
for(var z = 0; z < fields.length; z++){
field = fields[z];
name = field.name;
value = field.value;
type = field.type;
if(typeof name == "undefined" || name == ""){continue;}
if(type == 'checkbox' || type == 'radio'){
if(field.checked){
res += name +"="+ encodeURIComponent(value) +"&";
}
continue;
}
if(type == "select-multiple"){
for(var so = 0; so < field.length; so++){
if(field[so].selected){
res += name +"="+ encodeURIComponent(field[so].value) +"&";
}
}
continue;
}
if(value){
res += name +"="+ encodeURIComponent(value) +"&";
}
}
return res.substring(0, res.length - 1);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment