1

I have the following code that I have migrated from .NET to .NET Core.

Unfortunately, the javascript files are not being loaded with the loadjsfile. What can I use to be able to load these javascript files into my View Class?

<script>
    function loadjsfile(filename) {
        var fileref = document.createElement("script");
        fileref.setAttribute("type", "text/javascript");
        fileref.setAttribute("src", filename);
        document.writeln(fileref.outerHTML);
    };
    if (jQuery == undefined) loadjsfile(@Url.Content("~/Scripts/jquery-1.11.2.min.js"));

    loadjsfile(@Url.Content("~/Scripts/jquery.validate.min.js"));
    loadjsfile(@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js"));
    loadjsfile(@Url.Content("~/Scripts/contactusform.js");

</script>

Currently, I know for sure that "contactusform.js" is not being loaded because I call a javascript method in one of the input buttons and it says that the method cannot be found.

4
  • 1
    This is Javascript, and not really anything to do with core. All you probably need to worry about it the Url.Content bits. Check your web browser console for any errors.
    – DavidG
    Aug 17, 2016 at 16:58
  • What does loadjsfile do ? Why not use script tag to include your js files ?
    – Shyju
    Aug 17, 2016 at 16:59
  • @Shyju Looks like it loads jQuery if it's not already been defined. Could be handy for CDN going down for example.
    – DavidG
    Aug 17, 2016 at 17:00
  • @DavidG previously the code was loadjsfile("@Request.Url.GetLeftPart(UriPartial.Authority)/Scripts/jquery-1.11.2.min.js"); and I was trying to implement it into .NET Core, do you think that I should just get rid of it? Aug 17, 2016 at 17:03

1 Answer 1

4

If loadjsfile method take a string value, you should wrap the result of your Url.Content call in quotes.

loadjsfile("@Url.Content("~/Scripts/jquery.validate.min.js")");

You may also consider using the script tag helper with asp-fallback-src attribute.

<environment names="Staging,Production">
    <script src="//ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.4.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery">
    </script> 
</environment>

Update the asp-fallback-src value to your local copy of the library.

4
  • 2
    How would this help if (and this is guessing) the code is inside a partial view and jQuery was supposed to be added outside? Edit: Your edit is more likely the right answer, the extra info is a useful addition though.
    – DavidG
    Aug 17, 2016 at 17:05
  • I have previously tried to wrap it in quotes just like you have it in your answer but it did not change the result. I should have mentioned it in my question. maybe the connection problem is somewhere else... Aug 17, 2016 at 17:10
  • 2
    So i suggest you look into the code of loadjsfile method and see what it tries to do. As i mentioned in the answer, you may also consider using the tag helpers
    – Shyju
    Aug 17, 2016 at 17:20
  • 1
    I think wrapping it in quotes actually fixed the issue now, I was just not aware of the change of errors, now it is not a connection error but something else. I will try to implement the tag helpers as well Aug 17, 2016 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.