Answered by:
How to share the external Javascript file between razor view?

Question
-
User-1651604128 posted
Hi all,
This maybe a simple and common question, but I can not make it work, appreciate if anybody can help me out, much appreciated,
in my mvc web app, I have some JavaScript codes can be shared by more than one razor views, so in stead of use the same codes in may view, I want to share it in a javascript file.
Based on my understanding, I just need to add the javascript reference file in _Layout.cshtml, to be shared by other views. Here is my reference:
Here is the <head> section of _Layout.cshtml (the product.validation.js is my javascript files contains the javascript function to be shared by views.)
<head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <title>@ViewBag.Title - @GlobalRes.ApplicationName</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") <link href="~/Content/theme.css" rel="Stylesheet" type="text/css" /> <link href="~/Content/theme.min.css" rel="Stylesheet" type="text/css" /> <link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" /> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script> <script src="@Url.Content("~/Scripts/product.validation.js")" type="text/javascript"></script> </head>
and in my product.validation.js , I have this simple function:
function validatePro(thisVal) { if (thisVal != "" & thisVal.length != 3) { alert("Invalid product Code, Product Code must be 3 characters"); } }
And in each rezor view, I have this javascript codes
<script type="text/javascript"> $(function () { $('#ProID').blur(function () { var Pro = $('#ProID').val(); validatePro(Pro); }) }) </script> <table> <tr> <td class="AddrisLabel">Product Code:</td> <td> <input id="ProID" name="txtFilterProd" type="text" /> </td> </tr> </table>
and this is the bottom of _Layout view,
... @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html>
But when I run it, I have error message, the Javascipt can not find the validatePro function, it seems the external JavaScript file can not be found.
Any idea what is wrong with my case? thanks a lot again
Tuesday, June 18, 2019 2:12 PM
Answers
-
User-37275327 posted
<script src="@Url.Content("~/Scripts/product.validation.js")" type="text/javascript"></script>
Suspecting this is a path issue. Just comment out the above, and drag & drop the .js file directly to _Layout file form the solution explorer and check.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 18, 2019 2:28 PM -
User753101303 posted
Hi,
Use F12 Network to confirm a 404 not found and if that double check the path you are using is correct (and correctly generated)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 18, 2019 2:32 PM
All replies
-
User-37275327 posted
<script src="@Url.Content("~/Scripts/product.validation.js")" type="text/javascript"></script>
Suspecting this is a path issue. Just comment out the above, and drag & drop the .js file directly to _Layout file form the solution explorer and check.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 18, 2019 2:28 PM -
User753101303 posted
Hi,
Use F12 Network to confirm a 404 not found and if that double check the path you are using is correct (and correctly generated)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 18, 2019 2:32 PM