I want to write a small function that takes a URL as a parameter, but before loading that URL, it checks if it is already on that page. If so, it returns false
and nothing happens; otherwise, it loads the specified URL. I tried something like this:
function load(url) {
if(window.location.href === url) {
return false;
} else {
window.location.href = url;
}
}
Unfortunately, if my current page is http://www.example.org/index.html, then, while calling load('http://www.example.org/index.html')
does nothing, when I call load('index.html')
, it reloads the page. How can I add the extra functionality to catch relative addresses as well as absolute ones?
EDIT: I want to make this dynamic, so that I can use this on any page, without knowing the base of its URL.