How to get current url path example in Jquery?

Today now in this post i will show you How to get current url path example in Jquery? Sometimes we may need to get the current URL in our jquery file. We can get the current URL or path of our current page in javascript by using “window.location”.

Now In this post i will give you three best way to get the current url in your js file as there are in the bellow listed:

  • window.location.href
  • $(location).attr(“href”)
  • window.location

Ok, so we can check the bellow example use in code and then run in your system.

Example:
<html lang="en">
<head>
    <title>JQuery - get current URL Example</title>
    <script src="https://code.jquery.com/jquery-2.2.2.min.js"></script>
</head>
<body>
<script type="text/javascript">
  var currentURL = window.location.href;
  alert(currentURL);
  var currentURL = $(location).attr("href");
  alert(currentURL);
  var currentURL = window.location;
  alert(currentURL);
</script>
</body>

Read Also: How we can create virtual host in ubuntu apache?

Thanks for read. I hope it help you. For more you can follow us on facebook

Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.