How to get current url with or without query string example jQuery
In this post we will give you information about How to get current url with or without query string example jQuery. Hear we will give you detail about How to get current url with or without query string example jQueryAnd how to use it also give you demo for it if it is necessary.
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 :
- <htmllang="en">
- <head>
- <title>JavaScript - get current URL Example</title>
- <scriptsrc="https://code.jquery.com/jquery-2.2.2.min.js"></script>
- </head>
- <body>
- <scripttype="text/javascript">
- var currentURL = window.location.href;
- alert(currentURL);
- </script>
- </body>
- </html>
<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>
Example 2: $(location).attr(“href”)
This is second example to get current URL in jQuery :
- <script type="text/javascript">
- var currentURL = $(location).attr("href");
- alert(currentURL);
- </script>
<script type="text/javascript"> var currentURL = $(location).attr("href"); alert(currentURL); </script>
Example 3: $(location).attr(“href”)
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 = window.location; alert(currentURL); </script>
Get Current URL without query string
Example 1:
- <script type="text/javascript">
- var currentURL=location.protocol +'//'+ location.host + location.pathname;
- alert(currentURL);
- </script>
<script type="text/javascript"> var currentURL=location.protocol + '//' + location.host + location.pathname; alert(currentURL); </script>
Example 2:
- <script type="text/javascript">
- var currentURL=window.location.href.split('?')[];
- alert(currentURL);
- </script>
<script type="text/javascript"> var currentURL=window.location.href.split('?')[0]; alert(currentURL); </script>
Now you can check this script in your application where you need it.
Hope this code and post will helped you for implement How to get current url with or without query string example jQuery. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs