How to Get the Current Page URL in jQuery?

jquery

Nicesnippets

534

27-12-2019


Hi Guys,

In this example,I will learn you how to get current page url in jquery.you can simply get current page url in jquery.

you will check to current page url,host name,base pathname and origin etc.bellow example

Example 1 :- Check Full Current page Url

In this example you will get the full url current page in jquery

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Check Current Page URL Using jQuery - nicesnippets.com</title>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$(".checkUrl").click(function(){

var fullURL = $(location).attr("href");

alert(fullURL);

});

});

</script>

</head>

<body>

<p>Check Current Page URL Using jQuery - nicesnippets.com</p>

<button type="button" class="checkUrl">Check URL</button>

</body>

</html>

Example 2 :- Check Base Current page Url

In this example you will get the base url current page in jquery

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Check Current Page URL Using jQuery - nicesnippets.com</title>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$(".checkUrl").click(function(){

var baseURL = $(location).attr("origin");

alert(baseURL);

});

});

</script>

</head>

<body>

<p>Check Current Page URL Using jQuery - nicesnippets.com</p>

<button type="button" class="checkUrl">Check URL</button>

</body>

</html>

Example 3 :- Check Pathname Current page Url

In this example you will get the pathname current page in jquery

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Check Current Page URL Using jQuery - nicesnippets.com</title>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$(".checkUrl").click(function(){

var pathname = $(location).attr("pathname");

alert(pathname);

});

});

</script>

</head>

<body>

<p>Check Current Page URL Using jQuery - nicesnippets.com</p>

<button type="button" class="checkUrl">Check URL</button>

</body>

</html>

Example 4 :- Check Host name Current page Url

In this example you will get the host name current page in jquery

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Check Current Page URL Using jQuery - nicesnippets.com</title>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>

<script type="text/javascript">

$(document).ready(function(){

$(".checkUrl").click(function(){

var host = $(location).attr("host");

alert(host);

});

});

</script>

</head>

<body>

<p>Check Current Page URL Using jQuery - nicesnippets.com</p>

<button type="button" class="checkUrl">Check URL</button>

</body>

</html>

It will help you....


Recommended Posts