Skip to content

Javascript Online Tool Project to Remove HTTP, HTTPS and WWW Protocol From URL

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Remove HTTP & HTTPS WWW Protocol From URL in Javascript</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
    <div class="container">
        <h1 class="text-center">
            REMOVE HTTP HTTPS WWW PROTOCOL FROM URL
        </h1>
        <form id="form">
            <div class="form-group">
                <label for="urls">Enter Urls:</label>
                <textarea class="form-control" id="urls" cols="30" rows="10"></textarea>
            </div>
            <div class="form-group">
                <button class="btn btn-danger btn-block">
                    Get Result
                </button>
            </div>
        </form>
        <div class="form-group">
            <label for="result">Result:</label>
            <textarea class="form-control" id="result" cols="30" rows="10"></textarea>
        </div>
    </div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
    $("#form").submit((e) => {
        e.preventDefault()

        var urls = $('#urls').val().split('\n');
        console.log(urls)

        let correctUrls = []

        urls.forEach(url => {
            correctUrls.push(removeProtocol(url))
        });

        console.log(correctUrls)

        correctUrls.forEach(url => {
            $("#result").append(url)
            $("#result").append("\n")
        });

    })

    function removeProtocol(url){
        // startsWith

        if(url.startsWith("www.")){
            const www = "www."
            return url.slice(www.length)
        }

        if(url.startsWith("https://www.")){
            const https = "https://www."
            // slice() method
            return url.slice(https.length)
        }

        if(url.startsWith("http://www.")){
            const http = "http://www."
            return url.slice(http.length)
        }

        if(url.startsWith("https://")){
            const https = "https://"
            return url.slice(https.length)
        }

        if(url.startsWith('http://')){
            const http = "http://"
            return url.slice(http.length)
        }

        // url is correct

        return url
    }
</script>
</html>