Cha
Cha

Reputation: 3

Why isn’t this working href="javascript"?

chrome browser

버전 : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36

onclick link

<!DOCTYPE html>
<html>
<body>
<script>
function winopen(url){
    window.open(url, '_blank');
    }
</script>
<p>Open link in a new window or tab: <a href="javascript:winopen('https://www.w3schools.com');" target="_blank">Visit W3Schools!</a></p>

</body>
</html>

O : https://www.w3schools.com window open
X : about:blank window open

Upvotes: 0

Views: 697

Answers (2)

Gleb Gorokhov
Gleb Gorokhov

Reputation: 41

You want to programmatically change the page, so the tag <a> is not correct. Replace your link with a button like that:

<button type="button" onclick="winopen('https://www.w3schools.com')">
  Visit W3Schools!
</button>

Or if you want to use the link but change the page programmatically for some reason, you can do it like that:

<script>
  function winopen(event) {
    event.preventDefault();
    window.open(event.target.url, event.target.target);
  }
</script>

<a href="https://www.w3schools.com" target="_blank" onclick="winopen">
  Visit W3Schools!
</a>

The winopen script takes the url and target from the link's attributes.

Upvotes: 0

Het
Het

Reputation: 54

I guess you are trying to open a link in a blank window? if you try changing your href so it points directly to the link.

HTML

    <!DOCTYPE html>
<html>

<body>
    <script>
        function winopen(url) {
            window.open(url, '_blank');
        }
    </script>
    <p>Open link in a new window or tab: <a href="https://www.w3schools.com" target="_blank">Visit W3Schools!</a></p>

</body>

</html>

Upvotes: 0

Related Questions