I am using jQuery. How do I get the path of the current URL and assign it to a variable? Example URL: http://localhost/menuname.de?foo=bar&number=0
In jQuery, you can use the window.location.href
property to get the current URL and assign it to a variable. Here's how you can do it using the example URL you provided:
html
<!DOCTYPE html>
<html>
<head>
<title>Current URL Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
var currentURL = window.location.href;
console.log("Current URL:", currentURL);
});
</script>
</head>
<body>
<!-- Your HTML content here -->
</body>
</html>
In this example, the window.location.href
property holds the complete URL of the current page. The $(document).ready()
function ensures that the JavaScript code is executed after the document is fully loaded.
When you open the HTML file in a browser, the JavaScript code will retrieve the current URL (http://localhost/menuname.de?foo=bar&number=0
in your example) and log it to the browser's console.
Remember to include the jQuery library using the <script>
tag before using jQuery functions.
Comments
Post a Comment