Get the current URL with JavaScript?

后端 未结 23 1967
梦谈多话
梦谈多话 2020-11-21 07:08

All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a va

23条回答
  •  忘了有多久
    2020-11-21 07:59

    Nikhil Agrawal's answer is great, just adding a little example here you can do in the console to see the different components in action:

    If you want the base URL without path or query parameter (for example to do AJAX requests against to work on both development/staging AND production servers), window.location.origin is best as it keeps the protocol as well as optional port (in Django development, you sometimes have a non-standard port which breaks it if you just use hostname etc.)

提交回复
热议问题