1

I have a bit of code that works fine if it is added to a script tag on a page. I've moved it into a seperate JS file (in the same folder as the HTML page) but I get an 'Object expected' error anytime I try to call it.

This is my HTML page:

<!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">
<head>
    <title>Untitled Page</title>
    <script type="text/javascript" src="Jscript1.js" />
    <script type="text/javascript">
        function t()
        {
            nsTest.test();
        }

        function t2()
        {
            nsTest.test2();
        }
    </script>
</head>
<body>
    <input type="button" value="test" onclick="t()" />
    <input type="button" value="test2" onclick="t2()" />
</body>
</html>

and this is my JS file:

var nsTest = function ()
{
    var test = function ()
    {
        alert('nsTest.test');
    }

    var test2 = function ()
    {
        alert('nsTest.test2');
    }

    return {
        test: test,
        test2: test2
    }
} ();

I'm sure that I'm missing something really simple and obvious but I'm pretty new to JS and I've been going round in circles for a couple of hours at this point.

Can somebody please let me know what I'm doing wrong?

Thanks,

David

4
  • Jscript1.js is probably not loading. Check firebug network tab, or just try to load it from the same location into the browser. Commented May 3, 2011 at 13:17
  • if you use a closure like this (it's not a class), you don't have to name it. There's no point in naming nsTest. Commented May 3, 2011 at 13:33
  • @Rudie: The function is not named. The return value of the function is assigned to nsTest. Commented May 3, 2011 at 13:35
  • @Felix You are right. I and Headshota are wrong. Commented May 3, 2011 at 15:01

3 Answers 3

3
<script type="text/javascript" src="Jscript1.js" />

is XML not HTML.

So you don't have a complete script tag, which can screw up the definitions in the following script tag.

Change it to

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

If that doesn't solve the problem (i.e. if that DTD is a real XHTML DTD), then "JScript1.js" is not being served properly. Maybe try loading it in your browser to check that it's being served, and served with a mime-type like text/javascript.

Sign up to request clarification or add additional context in comments.

1 Comment

Adding the closing script tag seems to have fixed my problem.
2
(function nsTest (){
    var test = function (){
      alert('nsTest.test');
    }

    var test2 = function (){
      alert('nsTest.test2');
    }

return {
    test: test,
    test2: test2
}
}) ();

you must enclose your function in parentheses when doing immediate call.

4 Comments

You don't have to but it is better for compatibility.
I usually also don't name my closures, but that should be possible.
Named closures can cause leaks into the global scope in IE6 due to spec violations in that browser. E.g. in IE6, var x; (function x() {})(); alert(typeof x); will popup "function" instead of "undefined". I have not tested this in more modern versions of IE.
Actually this is not a good answer. (Damn I upvoted!) You're defining the function named within the scope you're executing it (or however the hell you say that!?). What the OP tries to do, is execute the sope and then assign it to nsTest. In your answer, nsTest is useless. In the OP's question, it's not.
0

I found this looking for help with the same problem. In my case the issue was that my tag had type="application/javascript" instead of type="text/javascript".

That worked in most browsers but made IE 8 error out with "Object Expected".

type="text/javascript" is the correct attribute.

Hope that helps someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.