-2

I'm trying to run a basic JavaScript function from an external file but I'm getting inconsistent results. Basically, I can't get a button's "onclick" event to fire when I put the script in an external JS page. I can get it work in CodePen:

CodePen

nonsense code

but NOT in JSFiddle:

JS Fiddle Examlple

I can always get it work when the script is part of the HTML page but I don't want to do that. Can you help? Thanks!

1 Answers1

-1

jsfiddle puts the javascript code in its own context:

//<![CDATA[
window.onload=function(){
    function clickFunction()
    {
      alert("this is working");
    }
}//]]> 

But codepen puts the js in the global scope.

Naftali
  • 144,921
  • 39
  • 244
  • 303