Pages

URL ?

const aString = 'https://www.hostinger.com/tutorials/wp-content/uploads/sites/2/2021/08/how-to-make-a-website-1.png'

const url = new URL(aString);
console.log(url)

The URL interface is used to parse, construct, normalize, and encode URLs. It works by providing properties which allow you to easily read and modify the components of a URL.

🥷Exemple d'utilisation de new URL dans un fichier index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module" src=""></script>
    <script>
        const parsedUrl = new URL(window.location.href);
        console.log(pareseUrl)
        document.body.innerText = `window.location.href = ${parsedUrl}`
    </script>
</body>
</html>