1

I would like to add the async property to the resulting script tag when I use the @Html.Script Razor syntax

@if (!SitefinityContext.IsBackend) { @Html.Script("/Path/to/javascript/file/products.min.js", "bottom") }

I know there is also this one:

@Html.Raw("<script src='/Path/to/javascript/file/products.min.js' async></script>")

But with this I can't specify the "bottom" parameter as above. Any idea?

  • Try this: `@if (!SitefinityContext.IsBackend) {@Html.Script(Url.Content(@Scripts.RenderFormat(@"", "/Path/to/javascript/file/products.min.js").ToString()), "bottom")}` – Rahul Sharma Jun 26 '19 at 11:37
  • Hm. It puts a red line under @Scripts and tells me "The name Scripts does not exist in the current context". – Bernd Basmer Jun 26 '19 at 13:17
  • Okay, this link can help you: https://stackoverflow.com/questions/23534516/the-name-scripts-does-not-exists-in-the-current-context-in-mvc – Rahul Sharma Jun 26 '19 at 13:32
  • Unfortunately this doesn't work either. I tried this now: ``` {@Html.Script(Url.Content(String.Format(@"", "/Link/to/Javascript/products.min.js").ToString()), "bottom")}``` But that results in this: ``` " type="text/javascript"> ``` – Bernd Basmer Jun 27 '19 at 11:54
  • Okay, can just try putting in the format without the `Url.Content`. Something like this: `{@Html.Script(String.Format("", "/Link/to/Javascript/products.min.js"), "bottom")}` – Rahul Sharma Jun 27 '19 at 12:00
  • I guess this can't work because the Html.Script already generates a script tag and as long as I have another – Bernd Basmer Jun 27 '19 at 12:12
  • Okay. I understand why this is happening. Have you tried to create a new "placeholder area" in your master layout cshtml file right above the closing body tag using `@Html.SfPlaceHolder("Bottom")`. This will create a draggable area in Sitefinity page edit mode, so you could drag any widget you want into this area including Javascript widget (for any js code). – Rahul Sharma Jun 27 '19 at 12:33
  • Yes, this was the idea I also had. I think this is the only way we can handle this. Thanks for your help anyway! – Bernd Basmer Jun 28 '19 at 09:23
  • Did you find a solution for this? – Rahul Sharma Jul 16 '19 at 11:57
  • Sorry for the late answer. No, I didn't find a solution to do it programmatically so I skipped this for now and decided to tackle it together with our .Net developer when he has time. For now creating the new placeholder area is the only way I could handle it and that's totally fine. – Bernd Basmer Aug 17 '19 at 07:49

0 Answers0