Ajaxify This Code

I have some code here which updates my post query but the problem is that it reloads the page every time which I don’t want to happen. Does anyone know how I can turn this piece of code to an ajax request? Any help would be greatly appreciated.

Here is the HTML:

<li id=”search_all” data-owner=”search_all” class=”<?php echo $view_all; ?>”><a onclick=”dontReload();”>Search All</a></li>
<li id=”dealerships” href=”&dealerships” data-owner=”dealerships” class=”<?php echo $view_dealer; ?>”><a>Dealerships</a></li>
<li id=”private_sellers” href=”&private_sellers” data-owner=”private_sellers” class=”<?php echo $view_private; ?>”><a>Private Sellers</a></li>

</ul>

And here is the JS:

function privateURL() {
var url=window.location.href,
separator = (url.indexOf(“?”)===-1)?”?”:”&”,
newParam=separator + “private_sellers”;
newUrl=url.replace(newParam,””);
newUrl+=newParam;
window.location.href = newUrl;

if(window.location.href.indexOf(“?dealerships”) > -1) {
mynewUrl = url.substring(0, url.length – 12) + ‘?private_sellers’;
window.location.href = mynewUrl;
} else if(window.location.href.indexOf(“#”) > -1) {
mynewUrl = url.substring(0, url.length – 1) + ‘?private_sellers’;
window.location.href = mynewUrl;
} else if(window.location.href.indexOf(“&dealerships”) > -1) {
mynewUrl = url.substring(0, url.length – 12) + ‘&private_sellers’;
window.location.href = mynewUrl;
}
}

function dealerURL() {
var url=window.location.href,
separator = (url.indexOf(“?”)===-1)?”?”:”&”,
newParam=separator + “dealerships”;
newUrl=url.replace(newParam,””);
newUrl+=newParam;
window.location.href =newUrl;

if(window.location.href.indexOf(“?private_sellers”) > -1) {
mynewUrl = url.substring(0, url.length – 16) + ‘?dealerships’;
window.location.href = mynewUrl;
} else if(window.location.href.indexOf(“#”) > -1) {
mynewUrl = url.substring(0, url.length – 1) + ‘?dealerships’;
window.location.href = mynewUrl;
} else if(window.location.href.indexOf(“&private_sellers”) > -1) {
mynewUrl = url.substring(0, url.length – 16) + ‘&dealerships’;
window.location.href = mynewUrl;
}
}

function searchAllURL() {
var url = window.location.href;

if(window.location.href.indexOf(“?private_sellers”) > -1) {
mynewUrl = url.substring(0, url.length – 16);
window.location.href = mynewUrl;
} else if(window.location.href.indexOf(“&private_sellers”) > -1) {
mynewUrl = url.substring(0, url.length – 16);
window.location.href = mynewUrl;
} else if(window.location.href.indexOf(“?dealerships”) > -1) {
mynewUrl = url.substring(0, url.length – 12);
window.location.href = mynewUrl;
} else if(window.location.href.indexOf(“&dealerships”) > -1) {
mynewUrl = url.substring(0, url.length – 12);
window.location.href = mynewUrl;
}

}

Read more here:: Ajaxify This Code

Leave a Reply

Your email address will not be published. Required fields are marked *