SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error with Included JS File

    Hi guys, i've got some javascript that works fine when it's all in the same script block, but when i divide it up and include the second file, it falls on its face with an 'Object Expected' Error at the foot of the page (little yellow road sign in IE).

    Driving me nuts. The code follows:

    -- MAIN PAGE --

    [script type="text/javascript" language="javascript" src="http://localhost:1186/TestSystem/ClientInterop/ClientModel.js"/]
    [script type="text/javascript" language="javascript"]

    function Run()
    {
    try
    {
    alert("running");

    var val = new JSNamedVal();

    alert(val);

    val.Name = "Name";
    val.Value = "Tristan";

    alert(val.Name);
    alert(val.Value);

    }
    catch (err)
    {
    alert(err);
    }
    }

    [/script]

    -- INCLUDED PAGE -- (..TestSystem/ClientInterop/ClientModel.js)

    function JSNamedVal(name, value)
    {
    alert("New NV");
    this.Name = name;
    this.Value = value;
    }

    However, if i move this function to the main page, it totaly works as intended.

    Can anyone explain to me why it's doing this? And what i can do about it?

    Cheers.

    Tris

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,153
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    object expected

    Hi trhodes, welcome to the forums,
    It sounds like it has something to do with the way browsers load and execute scripts. IE executes external scripts after inline scripts. http://www.websiteoptimization.com/speed/tweak/defer/ So when it looks for the object and the external script hasn't executed yet you get the error.
    You may be able to hold of the function with a window.onload to call it if you're not calling it this way already.

  3. #3
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, i toyed around with a couple of solutions i googled for deferring / delaying the execution, and in the end moved the script to execute on a button click, but i still get the same error.




    Tristan

  4. #4
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    omg... wtf?

    I seperated the end tag.... just changed the [script/] to [script] [/script] and now it works....

    what a stupid error.

    T

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,153
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    self-close script tag

    Good catch, I didn't see that.

  6. #6
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this a known problem?

    I just randomly stumbled on it using Thomas Edisons technique


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •