简体   繁体   中英

Javascript works inline but not in a file

I am new to javascript and am assuming that the answer is simple and right under my nose, but I can't seem to find the proverbial dog that's going to bite me.

Anyway, this works inline:

<script language="javascript">
    function foo(){

    alert("BOO!")

    }
</script>

<body onload=foo()>...

But if I do:

<script type="text\javascript" scr="js/blah.js">
</script>

<body onload=foo()>

blah.js contains:

function foo(){

    alert("BOO!")

    }

That doesn't work.

Why?

正确的语法是:

<body onload="foo();">

The type is text/javascript ( / instead of \\ ), and the onload attribute in the body element should be onload="foo();" .

Also, is your blah.js file stored inside a directory called js?

Inside markup you can call JavaScript functions. There is nothing wrong with it. This creates a new anonymous function in the DOM tree and adds the value of the Event model as the function body.

<script type="text\javascript" scr="js/blah.js"> </script>

should be

<script type="text/javascript" src="js/blah.js"> </script> // / and src

should be

<body onload="foo()"> // Markup attribute values should be passed inside quotes

所有属性值都必须以“开头”,并以“结尾”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM