简体   繁体   中英

JavaScript works locally but not when deployed to webserver

I'm deploying some code to a website and when I do the JavaScript does not run. I get the error:

SCRIPT5007: The value of the property '$' is null or undefined, not a Function object.

The code it is using is

@model MVCMasterDetail.DataAccess.cwwoLoadCurrentWorkOrderActualsResult[]

<script type="text/javascript">
  $(document).ready(function () {
    $(".grid-toggle").click(function () {
        $(this).closest("table").find("tbody").show();
    });
    $(".hide-rows").click(function () {
        $(this).closest("table").find("tbody").hide();
    });
});
</script>

@Html.Partial("MasterDetailMasterPartial")

And what calls uses it is:

<td colspan="4"> Details<br/><a href="javascript: void(0)" class="grid-toggle">Show-     </a><a href="javascript: void(0)" class="hide-rows">Hide</a></td>

Any help is appreciated.

Based off your comment in Ashley Ross's answer, it looks like your trying to add jQuery using a relative path. it also looks like you're using MVC. In MVC, the way you want to reference a file in html is:

<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")"></script>

Also, it doesn't matter if your script tag goes in the head tag or not. The only difference it makes is loading. By putting it in the head tag, you're telling the browser to download the js file before it begins to load the body, which can be nice, but can also increase page load times. For faster page download, you actually want to put your script tags towards the bottom of your body tag, which defers downloading the js file until after the rest of the page has loaded. This results in faster page load times, but can cause some funky behaviors if you aren't anticipating it.

You need to include jQuery before any other scripts. It sounds like you have something like

<script type="text/javascript" src="customScriptThatUsesjQuery.js"></script>
<script type="text/javascript" src="jquery-x.y.z.min.js"></script>

instead of

<script type="text/javascript" src="jquery-x.y.z.min.js"></script>
<script type="text/javascript" src="customScriptThatUsesjQuery.js"></script>

Check that it appears in your HTML source before any other scripts that use it.

您必须在<head>添加对jquery lib的引用

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM