开发者

onload event throws 'object expected' error

开发者 https://www.devze.com 2023-04-04 10:13 出处:网络
Whenever I use theevent, i get a javascript error saying \"Object expected\" pointing t开发者_高级运维o the line containing the onload event. Here is a simple representation of my webpage.

Whenever I use the event, i get a javascript error saying "Object expected" pointing t开发者_高级运维o the line containing the onload event. Here is a simple representation of my webpage.

<html>
<head>
  <script type='text/javascript' src='myScript.js'>
    //script containing doSomething()
  </script>
</head>
<body onload = "doSomething('stringArg', 0);"> //<--does not execute, throws error, EDITED for proper quotation

//body of site

<script type='text/javascript'>
  //test call
  doSomething('stringArg', 0); //<--executes just fine, no errors
</script>
</body>
</html>


You are using single quotes for everything so the parser thinks your attribute ends after 'doSomething('

Use double quotes to avoid confusion

<body onload = "doSomething('stringArg', 0);">

Hopefully not to confuse you, but in javascript, quotes are more or less interchangeable so you could also swap them:

<body onload = 'doSomething("stringArg", 0);'>

They just can't all be the same.


You have a syntax error (because of the single quotes being used for both the string literal and the attribute value). Try this:

<body onload = "doSomething('stringArg', 0);">
0

精彩评论

暂无评论...
验证码 换一张
取 消