<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="style.css">
    <script>
        function getBaseURL() {
            var currentBaseURL = location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + "/";            
            return currentBaseURL;
        };

        window.onload = function (e) {
            var baseURL = getBaseURL();
            alert(baseURL);
        };
    </script>
</head>
<body>
    <div>
        <h1>how to get base url in JavaScript</h1>
        <h2>http://www.code-sample.com/</h2>
    </div>
</body>
</html>
// Code goes here

/* Styles go here */