0

It works if I use:

<script>
    document.getElementById('download1').onclick = function() {
        window.location.href="https://youtube.com";
    };
</script>

But if I put it in a .js file it doesn't work!

I could use button onclick="window.open;"like this:

<button id="download1" class="button2">BUTTON</button>

But this should work for different files and I want to use one .js for this, so I can change the URL later and it would work for all files.

Leeroy
  • 5
  • 4
  • 5
    did you wrap your .js in a document.ready function, or include it at the bottom of your page? – Cruiser Feb 07 '17 at 22:03
  • 3
    make sure you are loading script after html is loaded, add it before `

    ` tag or after your content.

    – azs06 Feb 07 '17 at 22:03
  • Is the `.js` file referenced from a script tag? – Brian McCutchon Feb 07 '17 at 22:04
  • 4
    Have you tried debugging your code using the browser's built-in developer tools? I'm guessing that you're getting at least one error message that you haven't reported – j08691 Feb 07 '17 at 22:06
  • Guys I am stupid asf*... I see that the code is between Now it works, thank you for your help guys! – Leeroy Feb 07 '17 at 22:10

1 Answers1

0

1- include your js file in the bottom of the page

2- try this

document.getElementById('download1').addEventListener('click',function(){
            window.location.href="https://youtube.com";
        });
Hajji Tarik
  • 1,072
  • 7
  • 23