Scripts.Render() if not exists

I want the following call to render only if such call wasn't already declared in a parent view.

@Scripts.Render("~/Scripts/jQuery")

What's the best way to do so?

Answers:

Answer

Here is one way:

<script>
    if (!window.jQuery) {
        document.write('<script src="@BundleTable.Bundles.ResolveBundleUrl("~/Scripts/jQuery")">\x3C/script>');
    }
</script>

This is essentially the same logic used when one includes jQuery from a CDN, and then has a local reference fallback if the CDN delivery failed.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.