-1

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.

Frost
  • 41
  • 9
  • I'm not familiar with your use-case, but would a [HTTP 301 ("permanent redirect") header](https://en.m.wikipedia.org/wiki/HTTP_301) address this issue? – David Thomas Mar 22 '16 at 20:20

2 Answers2

1

You can use window.location to get some information regarding the current url:

window.location.origin in the console on this current page, prints:

"http://stackoverflow.com"

Then you could run some JS logic to check against your other url and use alert() to crete the pop up.

working JSBIN: https://jsbin.com/gijola/edit?js,console

adding code:

function checker (url) {
  var here = window.location.origin;
  l(here);
  if (here !== 'whatever you want to check') {
    alert('please update your bookmark!!');
  }
}
omarjmh
  • 13,632
  • 6
  • 34
  • 42
1

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
}
Jesse Lee
  • 1,291
  • 1
  • 8
  • 20