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.hrefThis is first way to get current URL in jQuery :
- <html lang="en">
- <head>
- <title>JavaScript - 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);
- </script>
- </body>
- </html>
This is second example to get current URL in jQuery :
- <script type="text/javascript">
- var currentURL = $(location).attr("href");
- alert(currentURL);
- </script>
This is third example to get current URL in jQuery :
- <script type="text/javascript">
- var currentURL = window.location;
- alert(currentURL);
- </script>
- <script type="text/javascript">
- var currentURL=location.protocol + '//' + location.host + location.pathname;
- alert(currentURL);
- </script>
- <script type="text/javascript">
- var currentURL=window.location.href.split('?')[0];
- alert(currentURL);
- </script>