Forum Moderators: open

Message Too Old, No Replies

Help! javascript "object expected" error in html body onload .

         

nelsonm

10:59 pm on Dec 6, 2010 (gmt 0)

10+ Year Member



Hi all,

I've been testing my new site for browser compatability when i ran into the "object expected" error at the body onload call of my javascript function "getImage()". The function is defined in the keypad.js source file. For the life of me, i can't figure out what is wrong.

I see this error in the IE Developer Tools debug screen while IE7 browser mode and standards is selected. Here is the code...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/content-wide-ext-charities.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Charities Form</title>
<!-- InstanceEndEditable -->
<?php include("/inc/meta.txt"); ?>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="application/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

<!-- InstanceBeginEditable name="head" -->
<?php include("scripts/volunteer-db.php"); ?>
<link href="css/keypad.css" rel="stylesheet" type="text/css"/>
<script type="application/javascript" src="scripts/keypad.js"></script>
<!-- InstanceEndEditable -->
</head>

<body onload="getImage()">

<!-- InstanceBeginEditable name="body" -->
<!-- InstanceEndEditable -->


Here is the function defined in the keypad.js source file...
function getImage() {
var num = Math.ceil(Math.random()*5);
var isrc = "images/bg-masthead-L" + num + ".jpg";

document.getElementById("banner-L").src = isrc;
}

Fotiman

1:07 am on Dec 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your scripts have a type defined that browsers don't recognize. Instead of:

type="application/javascript"

use

type="text/javascript"

nelsonm

1:40 am on Dec 7, 2010 (gmt 0)

10+ Year Member



thanks, that was it...

W3 say that "text/javascript" - is obsolete and is replaced by "application/javascript" as the current official MIME type for JS.

What do you do if you want to stay current but still be backward compatible with IE<8?

Can i test for IE < 8 use text/javascript?

astupidname

7:07 am on Dec 7, 2010 (gmt 0)

10+ Year Member



W3 say that "text/javascript" - is obsolete and is replaced by "application/javascript" as the current official MIME type for JS.

I have not heard any such thing, maybe I've not been listening closely enough or something but the html4 script element spec [w3.org] states:
type = content-type [CI]
This attribute specifies the scripting language of the element's contents and overrides the default scripting language. The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.


And the html5 script element spec [dev.w3.org] says:
The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".


So I'd be interested to see where you read that. All in all, though, stick with what works and is supported in all browsers ("text/javascript") of course, or can leave type attribute out if using html5.