How to Get the Current URL using JQuery

Introduction

Have you ever wondered how to get the current URL of a web page using JQuery? Well, you’re in luck! In this article, we will explore the different methods and techniques to achieve this. So, let’s dive right in!

Method 1: window.location.href

<html lang="en">    
<head>
    <title>JQuery - Get current URL</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
<script type="text/javascript">


  var currentURL = window.location.href;
  alert(currentURL);


</script>
</body>
</html>

Output:-

Method 2: $(location).attr(“href”)

<html lang="en">    
<head>
    <title>JQuery - Get current URL</title>
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
<script type="text/javascript">


  var currentURL = $(location).attr("href");
  alert(currentURL);

  
</script>
</body>
</html>

Output:-

Method 3: window.location

<html lang="en">    
<head>
    <title>JQuery - Get current URL</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
<script type="text/javascript">


  var currentURL = window.location;
  alert(currentURL);
</script>
</body>
</html>

Output:-

Hopefully, It will help you …!!!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x