Search code examples
javascriptbookmarks

javascript check which URL loaded and give notification


I'm trying to find a way in javascript to check which URL is loaded, then have a popup notifying the user to update their old bookmarket and have it redirect to the new location in a few seconds.

For example, the url maybe Http:\abc\myappage and I want to check if they are on the http:\abc site which if they are, the notification pops up and redirects them.

Currently I have a simple redirect to take them to the new site, but I never considered anyone that has an old bookmark which would never get updated if you don't inform them about the change.

Thanks.


Solution

  • You can access the current url from within JavaScript with window.location.

    Using window.location you can access the current domain and path, then by setting window.location.href = 'your new site' after a few seconds or after some user interaction will cause the browser to navigate to the supplied url.

    if(window.location.host === 'abc'){
        alert('This url is no longer valid.');
        window.location.href = 'http://abc/myappage
    }