basically a PHP car dealers site has links as this where last number in url car Id used to call a similar url underground like this 404.php called and in 404.php this exist…
If all your PHP script does is fetching and returning data from another server based on the current URL, this can indeed be done with JS.
var req = new XMLHttpRequest(),
// Get the car ID from the current URL
car_id = parseInt(window.location.href.match(/\d+$/));
// Listen to changes of the request's readyState
req.onreadystatechange = function() {
// When the readyState is 'DONE'
if (this.readyState === 4) {
// Log the response
console.log(this.responseText);
}
};
// Actually open and send the request. The response will be handled
// asynchronously by the above function
req.open('GET', 'http://www.yyyyy.com/carDetails.php?n=1&car_id=' + car_id);
req.send();
PS: You can check if the page does exist with this.status === 404 in the onreadystate handler.
only where cross domain access is permitted by the server AND where JavaScript is actually enabled. So not for everyone as it does with the server side version.
With PHP, you have control over the site’s functionality. If you rely on (front-end) JS to do all this, you have to hope that the end user has JS enabled, that JS doesn’t fail to load properly, etc. etc. What’s your rationale for switching to JS?
You’re not going to be able to load another sites content with Ajax. You need a server side proxy hosted on your own domain due to cross origin request policy restrictions.