0

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?

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
Alwyn
  • 8,079
  • 12
  • 59
  • 107
  • checkout http://stackoverflow.com/questions/12192646/why-use-scripts-render-bundles-jquery – zs2020 Feb 20 '13 at 01:55
  • 1
    generally you should design your bundles so they dont have overlaps, id suggest having one bundle for the main page and one for the child view – Daniel Powell Feb 20 '13 at 01:56
  • additionaly if you are loading jquery related libraries these should be added at the end of the page in most cases, so the master page/layout is the best place to do this. – Daniel Powell Feb 20 '13 at 01:57
  • This already is at the layout page, it's however an area local layout. It needs to detect if the parent layout already defines these things. I need the check at runtime. In short, yes I'm aware there're workarounds, but I want this to behave exactly as I described it. Is there a way to do so out of the box? I can run custom code if I have to, but prefer to work with the framework. – Alwyn Feb 20 '13 at 02:07

1 Answers1

2

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.

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89