The current location is in location.href .
The location object also contains some other useful fields:
- location.hash: part after # in URL
- location.host: hostname, including port (if specified)
- location.hostname: just the host name
- location.pathname: requested URI without protocol / host / port; beginning with /
- location.port: Port - only if specified in the URL
- location.protocol: Usually "http:" or "https:" is the mind at the end of the colon
In your case, the most fault tolerant way is to check if filename truck.php is:
var parts = location.pathname.split('/'); if(parts[parts.length - 1] == 'trucks.php') { location.href = 'some-other-page'; }
If you want to redirect without saving the current page in history, use the following code instead of assigning location.href :
location.replace('some-other-page');
source share