简体   繁体   中英

How to get javascript to render in views injected into my layout in Asp.Net?

I am creating a simple website with a single layout in Asp.Net MVC 4. The layout has Javascript tags in the head like so:

<script src=@Url.Content("/Scripts/jquery.min.js")></script>
<script src=@Url.Content("/Scripts/jquery.dropotron.min.js")></script>
<script type="text/javascript" src=@Url.Content("/Scripts/jquery.slidertron.min.js")></script>
<script src=@Url.Content("/Scripts/skel.min.js")></script>
<script src=@Url.Content("/Scripts/skel-layers.min.js")></script>
<script src=@Url.Content("/Scripts/init.js")></script>

This renders fine when I first load the website/page.

The project uses the typical URL/Home/Index route as the default - but when I call this explicitly by adding '/Home/Index' to the URL the page is rendered without any styling. Any page other than the home as it loads is rendered without style even though they are all just views being injected into this same layout with this same Javascript in the head.

Any ideas why the Javascript isn't being rendered in all of my views?

The only thing that can really cause this is using a relative URL to reference the file. When just loading the domain without a path, a reference like Scripts/foo.js will properly be retrieved at the URL: http://domain.com/Scripts/foo.js . However, with a path like /Home/Index , the URL then becomes http://domain.com/Home/Index/Scripts/foo.js , which is obviously not correct.

However, in your case here, you're not using completely relative paths, as each is prefixed with / which should cause it to always reference from the site root, despite the current URL path. If we are to assume that you copied and pasted this directly from your project, I'm a bit at a loss how you could be experiencing this issue. First, verify that the code you posted here is in fact what you have in your layout. Then, make sure that the views are using the correct layout, and that you've done silly things like saved the file in case you made changes recently. Short of that, try doing a hard refresh in your browser and/or clear the cache, to make sure it's not simply loading stale pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM