تغییر پارامترهای صفحه (Query String) بدون بارگزاری مجدد.
مختصر و مفید. میتونید راحت با کوئری استرینگ ها بازی کنید!
function getURLParameter(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null; } function changeUrlParam (param, value) { var currentURL = window.location.href+'&'; var change = new RegExp('('+param+')=(.*)&', 'g'); var newURL = currentURL.replace(change, '$1='+value+'&'); if (getURLParameter(param) !== null){ try { window.history.replaceState('', '', newURL.slice(0, - 1) ); } catch (e) { console.log(e); } } else { var currURL = window.location.href; if (currURL.indexOf("?") !== -1){ window.history.replaceState('', '', currentURL.slice(0, - 1) + '&' + param + '=' + value); } else { window.history.replaceState('', '', currentURL.slice(0, - 1) + '?' + param + '=' + value); } } } // call example changeUrlParam('myParameter', 'hello');
نکته
how to change URL Parameter without reloading page
نویسنده : محمد ملک مکان
دوشنبه 25 آبان 1394
دوشنبه 25 آبان 1394