Created
April 30, 2016 04:20
-
-
Save softsolution/537e745a4c20fca92a08e7744ef2e464 to your computer and use it in GitHub Desktop.
Push state function - изменение урла при ajax запросе
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
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