Using html button + javascript function to open links in new window


#1

Hi all,

Im trying to set up a function where once a html button is pressed, a function checks a variable and redirects a link to a new window if the value comes back positive.

This is the function I’m trying to get going:

     function launchTask()   
     {
        window.open("https://www.google.com","_blank");
    }

and the button

button type=“button” onclick=“LaunchTask()”

Theres probably a basic solution i’m not seeing, can somebody help?


#2

The first thing to check is that the browser isn’t preventing popup windows (which would most likely open in a new tab, anyway.)

Use DevTools (F12) and click on the “Console” tab. Click your button. If there are any JavaScript issues, there should be a warning appear.

HTH,

^ _ ^

UPDATE: Oh, and don’t use _blank as the window name. Use something else.


#3

You’ll see the error if you open the console of your dev tools – you misspelled the function name, it starts lowercased. Better though would be to avoid inline-JS altogether; your code gets much easier to debug if you have your logic separate from the markup:

HTML

<button type="button" id="redirect">redirect</button>

JS

var redirectBtn = document.getElementById('redirect')

redirectBtn.addEventListener('click', function launchTask () {
  window.open('...')
})

(x-post)


#4

I completely flaked on that. Did not see it at all. Good eye.

V/r,

^ _ ^