How to get current url with or without query string example jQuery

How to get current url with or without query string example jQuery

In most cases while working with JavaScript, we sometime need to get current URL with or without query string.

There are so many ways to get the current URL of web page.

You may need current URL at the time of redirection or you can perform so many other task with current URL.

In this post, i will tell you how to get current URL with or without query string in jQuery.

Example 1: window.location.href

This is first way to get current URL in jQuery :

  1. <html lang="en">
  2. <head>
  3. <title>JavaScript - get current URL Example</title>
  4. <script src="https://code.jquery.com/jquery-2.2.2.min.js"></script>
  5. </head>
  6. <body>
  7. <script type="text/javascript">
  8. var currentURL = window.location.href;
  9. alert(currentURL);
  10. </script>
  11. </body>
  12. </html>
Example 2: $(location).attr("href")

This is second example to get current URL in jQuery :

  1. <script type="text/javascript">
  2. var currentURL = $(location).attr("href");
  3. alert(currentURL);
  4. </script>
Example 3: $(location).attr("href")

This is third example to get current URL in jQuery :

  1. <script type="text/javascript">
  2. var currentURL = window.location;
  3. alert(currentURL);
  4. </script>
Get Current URL without query string Example 1:
  1. <script type="text/javascript">
  2. var currentURL=location.protocol + '//' + location.host + location.pathname;
  3. alert(currentURL);
  4. </script>
Example 2:
  1. <script type="text/javascript">
  2. var currentURL=window.location.href.split('?')[0];
  3. alert(currentURL);
  4. </script>

Phone: (+91) 8800417876
Noida, 201301