9

Possible Duplicate:
Creating a new Location object in javascript

If the user inputs a string that is a URL, what's the easiest way to turn it into a Location object (like window.location) so I can use methods and properties like obj.hostname?

Community
  • 1
  • 1
Kerrick
  • 7,420
  • 8
  • 40
  • 45
  • `location.constructor()` === `TypeError: Illegal constructor`. Interpreter says no. You can always create an iframe, navigate to the url in the iframe then access the `location` object in the iframe. (bad idea) – Raynos Jul 18 '11 at 17:14
  • @lwburk -- Yep, that's what I was trying to find with my 30 minutes of searching prior to posting this. >.> – Kerrick Jul 18 '11 at 17:16
  • Here is the perfect solution: http://stackoverflow.com/a/3213643/4477463 – Egor May 05 '17 at 03:53

1 Answers1

5

Don't know if you can create a Location object, but this article explains how to use an anchor tag for the same effect:

http://james.padolsey.com/javascript/parsing-urls-with-the-dom/

jimbo
  • 11,004
  • 6
  • 29
  • 46