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?

4
  • checkout stackoverflow.com/questions/12192646/… Commented Feb 20, 2013 at 1: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 Commented Feb 20, 2013 at 1: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. Commented Feb 20, 2013 at 1: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. Commented Feb 20, 2013 at 2:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes this seems like one possible solution. Thank you!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.