views:

51

answers:

2

Hi,

I have a html page like this

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <script src="JScript2.js" type="text/javascript"></script>
    <script src="JScript.js" type="text/javascript"></script>
    <title></title>
</head>
<body >
    <div ><input type=text id="uxTextBox" /></div>
</body>
</html>

and 2 javascript files like this

/// <reference path="JScript.js"/>
/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="jquery-1.3.2.js" />

$(document).ready(function() {
    referral.PopulateTextBox();

}

=====end of first========== file

/// <reference path="jquery-1.3.2-vsdoc.js"/>
/// <reference path="jquery-1.3.2.js" />


var referral = new Referral(); 
$(document).ready(function() {
function Referral() {
    this.PopulateTextBox = function() {
    $("#uxTextBox").text("some text");


    }
}

}

the problem is that neither of the 2 jquery files seem to execute. I am trying to populate an object from the call to another js files and then return the value to the html file

any ideas?

A: 

You'll need to make sure to include the jQuery source on your html page. The "reference" comments look like they are something inserted by your editor so it can figure out which files you are using, but those comments do not tell the browser where to get the jQuery source.

Put this as the first <script> tag in your document:

<script src="/path/to/jquery.js"></script>
TM
+2  A: 

It's about the scope, JavaScript has function-scope, so all the variables and function declarations that you do inside the $(document).ready callback function, are only accessible in that scope.

For example:

$(document).ready(function() {

  function Referral() {
    // ...
  }

  // Referral is accessible here
});
// But not here

You could declare your Referral constructor function in the global scope, as you intend to use it from multiple files.

And if you don't like globals you can implement a namespacing technique:

More info about the scope:

CMS