Get the current URL with JavaScript?

2018-12-31 01:53发布

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 variable to do with as I please.

20条回答
大哥的爱人
2楼-- · 2018-12-31 02:19

if you are referring to a specific link that has an id this code can help you.

$(".disapprove").click(function(){
                      var id = $(this).attr("id");

                      $.ajax({
                          url: "<?php echo base_url('index.php/sample/page/"+id+"')?>",
                          type: "post",
                          success:function()
                          {
                            alert("The Request has been Disapproved");
                            window.location.replace("http://localhost/sample/page/"+id+"");
                          }
                      });
                });

I am using ajax here to submit an id and redirect the page using window.location.replace. just add an attribute id="" as stated.

查看更多
与君花间醉酒
3楼-- · 2018-12-31 02:20

Gets the current page URL:

window.location.href
查看更多
回忆,回不去的记忆
4楼-- · 2018-12-31 02:24

OK, getting the full URL of the current page is easy using pure JavaScript. For example, try this code on this page:

window.location.href;
// use it in console of this page will return
// http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser"

The window.location.href property returns the URL of the current page.

document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;
<!DOCTYPE html>
<html>

<body>
  <h2>JavaScript</h2>
  <h3>The window.location.href</h3>
  <p id="root"></p>
</body>

</html>

Just not bad to mention these as well:

Also if you need a relative path, simply use window.location.pathname;

And if you'd like to get the host name, you can use window.location.hostname;

And if you need to get the protocol separately, simply do window.location.protocol

Also if you page has hash tag, you can get it like: window.location.hash

So window.locatation.href handles all in once... basically:

window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
    //true

Also using window no necessary if already in window scope...

So in that case you can use:

location.protocol

location.hostname

location.pathname

location.hash

location.href

Get the current URL with JavaScript

查看更多
情到深处是孤独
5楼-- · 2018-12-31 02:27

You can get the full link of the current page through location.href and to get the link of the current controller, use:

location.href.substring(0, location.href.lastIndexOf('/'));
查看更多
长期被迫恋爱
6楼-- · 2018-12-31 02:27

Firstly check for page is loaded completely in browser, then call a function which takes url, URL variable and prints on console,

$(window).load(function(){
   var url = window.location.href.toString();
   var URL = document.URL;
   var wayThreeUsingJQuery = $(location).attr('href');
   console.log(url);
   console.log(URL);
   console.log(wayThreeUsingJQuery );
});
查看更多
旧人旧事旧时光
7楼-- · 2018-12-31 02:31

Adding result for quick reference

window.location;

 Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
 ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ, …}

document.location

  Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript", 
ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ
, …}

window.location.pathname

"/questions/1034621/get-the-current-url-with-javascript"

window.location.href

"https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"

location.hostname

"stackoverflow.com"
查看更多
登录 后发表回答