jQuery

How to get the current URL using Jquery

In this tutorial, we are going to see how to get the current URL using Jquery. You can simply use the method attr() of jQuery to get the value of the attribute ‘href’. The following example will show you how to find the URL of the current page using jQuery.
 

How to get the current URL using Jquery
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>How to get the current URL</title>
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				$("button").click(function(){
					var url = $(location).attr("href");
					alert(url);
				});
			});
		</script>
	</head>
	<body>
		<button type="button">Get URL</button>
	</body>
</html>
Result
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *