jQuery
jQuery Get Current URL Examples (2 Ways)
Here is a little trick to get current url of a web page using jQuery.
How to Get Current URL using jQuery
You can do this in 2 ways using jQuery.
Method 1: Pure jQuery style
<script type="text/javascript">
$(document).ready(function() {
var currentUrl = $(location).attr('href');
alert (currentUrl);
});
</script>Method 2: Mix of jQuery and native JavaScript
<script type="text/javascript">
$(document).ready(function() {
var currentUrl= window.location.pathname;
alert (currentUrl);
});
</script>That’s it for now!
Simple, isn’t it?
Do you know of any other ways to to get current URL using jQuery? Feel free to suggest by commenting below.