Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't I open a link without http or https using window.open() in Javascript?

I can click "Open 1" button to open a full link in a new tab in Chrome.

When I click "Open 2" button, I can’t open a the link without http or https, the browser URL address displays http://localhost:53484/www.google.com.

How can I fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code A

<html>
<head>
    <script src="Js/jquery-3.6.3.min.js"></script>

    <script type="text/javascript">

        function open1() {
            window.open('http://www.google.com')
        }

        function open2() {
            window.open('www.google.com')
        }
    </script>
</head>

<body>

    <input type=button value="Open 1" onclick="open1()">

    <input type=button value="Open 2" onclick="open2()">

</body>

</html>

>Solution :

http or https are needed to open an external link (i.e. to access any website in the world wide web) – it won’t ever work without this, this is the official protocol. So if you have a link without it, every browser will automatically add it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading