JavaScript get current url


JavaScript get current url- Working with JavaScript we sometimes need to get current url of the address. There are several ways to get the current url of the web page. It can be useful when you are working with javascript and need to redirect on current url or want to perform some other action with url. Here in this tutorial we are going to explain the ways how we can get the current url with several example which might be helpful for us. Let us go with very basic examples which will make the things clear to understand.


JavaScript get current url

You can get the current url simply in javascript as –

JavaScript get current url :

<script type="text/javascript">
var curr_url = window.location.href;
</script>

The above example will give you the full url with query strings.

More About Current Url In JavaScript

Let us have more information about the current url in javascript-

JavaScript get current url parameters

Some times we only need the url parameters in javascript.

JavaScript get current url parameters:

<script type="text/javascript">
function getUrlParams(param) {
  var query = window.location.search.substring(1);
  var parts = query.split("&");
  for (var i=0;i<parts.length;i++) {
    var seg = parts[i].split("=");
    if (seg[0] == param) {
      return seg[1];
    }
  } 
  alert("Parameter not found.");
}
// suppose url is www.example.com?&q1=test1&q2=test2;
var x = getUrlParams(q1);
alert(x);// will give you test1
</script>

In the above example we have created a function which accepts parameter name from query string and will give you its value if it is found.

Javascript Current url protocal name

If you want to get the protocal you can use the below method to get the protocal from url in javascript.

JavaScript get query strings from url:

<script type="text/javascript">
function getQueryString(){
var proto = location.protocal;
alert(proto);// example http:: or https
}
</script>

location.protocal gives the protocal name from the current url example - http::, https::.The above example will give the protocal name from url.

Javascript Current url hostname

If you want to get the hostname only you can use the below method to get the hostname from url in javascript.

JavaScript get query strings from url:

<script type="text/javascript">
function getQueryString(){
var host_name = location.hostname;
alert(host_name);// such as  -  example.com
}
</script>

The above example will give the hostname from url. location.hostname is used to get the hostname from the current url in javascript.

JavaScript get current url without parameters

Some times we only need the url without parameters in javascript. Here is simple example to get the current url without parameters-

JavaScript get current url without parameters:

<script type="text/javascript">
function getCurrentUrl(){
var curr_url = location.protocol + '//' + location.host;
alert(curr_url);// example - www.demo.com 
}
</script>

In the above example will give you the current url without parameters.

JavaScript get query strings from url

If you want to get the query strings only you can get as below -

JavaScript get query strings from url:

<script type="text/javascript">
function getQueryString(){
var query_string = location.pathname;
alert(query_string);// 
}
</script>

The above example will give the query string from url. location.pathname will give you the query string from the current url in javascript.

JavaScript get current url


Advertisements

Add Comment

📖 Read More