A fix for window.location.origin in Internet Explorer

Internet Explorer does not have access to window.location.origin, which is a bummer because it is a pretty handy variable to have, but we can make it work with a fairly straight forward check because we access .origin;

if (!window.location.origin) {
  window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');

This should now have .origin set to what you would expect.

Share this on
  • Saturate

    You forgot that origin includes the port number, you code does not :-)

  • http://www.tosbourn.com/ Toby Osbourn

    Good spot! I have fixed the post, thanks!

  • John

    That’s a life saver right there.. Thanks!

  • ⓕⓣ

    maybe see about getting this added to modernizer or post small repro on github, or get onto the pollyfill radar. nice snippet.

  • http://www.tosbourn.com/ Toby Osbourn
  • ⓕⓣ


  • Sm0ove

    Your snippet fixed my issue within the blink of an eye! Thanks alot sir!

  • John Babak

    window.location.port may return “80”, not an empty string in some IE versions (window.location.host contains “:80″ in this case, too). http://stackoverflow.com/questions/13167302/did-ie10-change-the-definition-of-window-location-port

  • http://www.tosbourn.com/ Toby Osbourn

    Excellent spot John – I am going to post some updated code very soon.

  • Frank Fang

    Nice hack. Thanks.

  • Yuval Greenfield

    You might as well have a function for whenever you need to access location.origin


  • http://www.tosbourn.com/ Toby Osbourn

    Great idea!

    I guess the only issue with that is you need to remember to call this each time, with my code it just overrides the attribute if it doesn’t already exist.

  • Yuval Greenfield

    I guess I wanted it to be apparent in my code that a compatibility fix was used in case something was ever shuffled or used in a different context. E.g. on a site that doesn’t have your fix pasted, or in code on your own site that runs before the fix is applied.

  • http://www.tosbourn.com/ Toby Osbourn

    That makes sense!

  • zlog

    Shouldn’t “//” be “://”?

  • zlog

    Actually, ignore me. `protocol` seems to include it.

  • Gagan

    window.location.hostname + (window.location.port ? ‘:’ + window.location.port: ”)
    could be replaced by
    window.location.host ???

  • http://juancarlosfarah.com Juan Carlos Farah

    Yes. That’s what I’m using. Is there a potential issue with window.location.host?

  • http://www.tosbourn.com/ Toby Osbourn

    I am not sure if it is supported by older versions of IE, I would need to check but you could be right :-)

  • BradBarbin

    Thanks man

  • chai

    wouldn’t have thought of the port! great man, thx!

  • crazy

    thank you! It helped me a lot!

  • Gerardo Cruz

    Nice yo save me a lot if time

  • Mattia

    You make my day! tvtb

  • Andre Z Sanchez

    For anyone using NPM, there’s a nice little polyfill that implements this snippet here:

  • http://www.tosbourn.com/ Toby Osbourn

    Great share Andre!

  • Andrew J

    This is exactly what I was looking for. Brilliant! Thanks for sharing.

  • renish khunt

    Nice yo save me a lot if time. thank you so much.

  • Sudarshan

    Nyc.. Ty so much :-)

  • Ashraf

    thanks for this post……

  • Harsha

    works like a charm thank you :)