May 26, 2020
How to Open URL on Button Click in javascript
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="style.css"> </head> <body> <div> <input type="text" placeholder="Enter Url"> <button>Open</button> </div> <script src="script.js"></script> </body> </html> |
CSS:
1 2 3 4 5 6 7 8 9 10 |
div { display:flex; justify-content: center; } input, button { display: inline-block; padding: 10px; margin-top: 5px; } |
Javascript:
1 2 3 4 5 6 |
let btnOpen = document.querySelector('button'); let input = document.querySelector('input'); btnOpen.addEventListener('click', () => { window.open(input.value, '_blank', 'height=600px, width=600px'); }); |