Saturday, June 27, 2020

Back to basics with JavaScripts

Remembering a bit of coolness in JavaScripting...
Using JavaScript, we can set a links or a input buttons (etc...) event to a JS function by only using JavaScript rather than adding code in the control filed attribute. This is pretty cool.

Example:

<html>
<head>
    <script type="text/javascript">

    /*Wait for the page to load first*/
    window.onload = function () {
        var siteLink = document.getElementById("siteLinkId");

/*run when the link is clicked*/
        siteLink.onclick = function () {
            if (!alert("Make sure you are connected to the VPN")) {
                window.location.href = "https://test.mywebsite.com";
            }
            return false;
        }
    }
    </script>
</head>
<body>
    <a id="siteLinkId" href="http://www.google.com">Visit My Site</a>       
</body>
</html> 

No comments:

Post a Comment