I noticed that a JS-Library didn't work, when I put it directly in a template of a documenttype which is rendered somewhere in <body>. I know it is better to put it in the <head> or just before the closing , but sometimes it was the best to put a script like the following just beside it, where it's needed.
So now when your view loads, it's js include will get rendered correctly.
Note, this only works in a View, it will not work for partial views. If you need to include js in a partial view then you can create an extension to do so.
JavaScript libraries only in master.cshtml
Hi everyone
I noticed that a JS-Library didn't work, when I put it directly in a template of a documenttype which is rendered somewhere in
<body>
. I know it is better to put it in the<head>
or just before the closing , but sometimes it was the best to put a script like the following just beside it, where it's needed.In Umbraco 8 it worked very well. Now with Umbraco 9 I had to put `
` into the master.cshtml and had to make an if-statement that it doesn't load on every page.
Is that really the only way to make it work?
Thanks
if thes are view templates, you could define a secion in your master template which you then use in your view templates to add extra scripts.
Example, in master template
Then in your View template you can do the following
So now when your view loads, it's js include will get rendered correctly.
Note, this only works in a View, it will not work for partial views. If you need to include js in a partial view then you can create an extension to do so.
is working on a reply...