1

For WebExtensions, there are several methods to check a page's URL:

Which one to use when you just need to check the URL, and maybe redirect based on it? Which is easiest to code? Are there reasons for using the others?

serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 1
    I guess it depends on the context. Content scripts have access only to `location.href` the other methods are available only to background scripts. – Titus May 07 '17 at 16:05
  • @Titus: whichever works best. A content script would have the advantage of retrieving the data to compare from `storage` only once, but it could also provide this in the background. – serv-inc May 07 '17 at 16:06
  • I'm not sure what you mean. You can definitely redirect to a new page from a content script using `location.href`. Yes, `location` is read only but `location.href` is not. `web_accessible_resources` is used if you want to make your extension's content available, to make them accessible in a web page's context, I'm not sure what this has to do with anything. – Titus May 07 '17 at 17:36

1 Answers1

1

For your specific use case use the best method is

window.location.href

if you want to get only domain name instead of full URL, use

window.location.hostname

For complete guide on Window Location API please check https://developer.mozilla.org/en-US/docs/Web/API/Window/location

Note: You mentioned document.location.href which is not supported on some version of Firefox browser

imondal007
  • 104
  • 5
  • To redirect to a local file, you need to [set `web_accessible_resources`](http://stackoverflow.com/a/43834395/1587329) – serv-inc May 07 '17 at 17:26