views:

112

answers:

3

Before, I had this:

<head>
    <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script type="text/javascript">
        var optPrompt = "- Select One -";
        var subCats;
        var parentCats;
        var nextBtn;

        var ParentChanged = function() {
            ClearDescription();

            if (this.selectedIndex == 0) {
                $(subCats).html($("<option>").text(optPrompt));
            }


            $.getJSON('<%=Url.Action("GetChildCategories") %>', { parentId: $(this).val() },
                    function(data) {
                        subCats.options.length = 0;
                        $("<option>").text(optPrompt).appendTo(subCats);
                        $(data).each(function() {
                            $("<option>").attr("value", this.ID).text(this.Name).appendTo(subCats);
                        });
                    });
        }

        var DisplayDescription = function(catId) {
            $.ajax({
                url: '<%=Url.Action("GetDescription") %>',
                data: { categoryId: catId },
                dataType: 'html',
                success: function(data) {
                    $("p#categoryDescription").html(data);
                }
            });
        }

        var ChildChanged = function() {
            var catSelected = this.selectedIndex != 0;

            if (!catSelected) ClearDescription();
            else DisplayDescription($(this).val());
        }

        var ClearDescription = function() {
            $("p#categoryDescription").html('');
        }

        $(function() {
            parentCats = $("select#Category").get(0);
            subCats = $("select#Subcategory").get(0);
            nextBtn = $("input#nextButton").get(0);

            $(parentCats).change(ParentChanged);
            $(subCats).change(ChildChanged);
        });

    </script> 
</head>

Then I put all of my inline script into a file (myScript.js) and changed my HTML to this:

<head>
    <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script src="/Scripts/myScript.js" type="text/javascript"></script> 
</head>

And now nothing is working. I opened up my page in IE7 and it had a page error that read:

Line: 54
Error: Unknown name.

Line 54 happens to be the last line of my external javascript file.

What am I doing wrong?

+2  A: 

Did you put the < script > tag inside your myScript.js? If yes, remove them.

Your myScript.js should start with

var optPrompt = "- Select One -";

SolutionYogi
No I didn't make that mistake.
Ronnie Overby
+4  A: 

Am I right in saying that this is ASP.Net? If it is, inline scripts like:

<%=Url.Action("GetDescription") %>

cannot go in the external JavaScript file.

Phairoh
That's the problem - ASP.NET code inside a javascript reference won't be processed by ASP.NET on the server.
Jon Galloway
I bet that's the problem. I knew that, but I forgot that I had that in the script. Oh well, I guess I'll just leave the script in the page. I don't think there's anything else I can do.
Ronnie Overby
+2  A: 

Since you are now serving the js as a static file, rather than via your ASP, lines like

<%=Url.Action("GetChildCategories") %>

will no longer work as the server doesn't interpret them and replace them with the correct values. You'll need to hard-code them in the script, or leave those lines as inline scripts in the main page and set them as global variables which you can then reference from the external file.

Daniel Roseman