Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Ability to inject script tag into head element? #37

Open
fingers10 opened this issue Oct 23, 2022 · 7 comments
Open

Comments

@fingers10
Copy link

Current in blazor wasm index.html there is now way to get environment and load scripts based on environment. If we add a feature to this package to inject script into head tag, It will be awesome.

@jsakamoto
Copy link
Owner

@fingers10 Actually, injecting <script> elements dynamically is a more complicated task than you might think.
Instead, would the following package help you?

@Alerinos
Copy link

Alerinos commented Oct 24, 2022

@fingers10 You can use this method as well.

var module = await JS.InvokeAsync<IJSObjectReference>("import", "/lib/xxx.js");
await module.InvokeVoidAsync(xxx);

@fingers10
Copy link
Author

@fingers10 You can use this method as well.

var module = await JS.InvokeAsync<IJSObjectReference>("import", "/lib/xxx.js");
await module.InvokeVoidAsync(xxx);

@Alerinos does this approach adds the script tag to head element? I would like to just add an script tag with src pointing to cdn conditionally..

@Alerinos
Copy link

@fingers10 Why can't you do this in _Layout?

@fingers10
Copy link
Author

@fingers10 Why can't you do this in _Layout?

@Alerinos do you mean MainLayout.razor in blazor wasm?

@jsakamoto
Copy link
Owner

Hi @Alerinos , Thank you for helping us!

By the way, I guess that @fingers10 is talking about his website "https://ilovedotnet.org/" (The source code is here). And that website is hosted on GitHub Pages. The website is a Blazor WebAssembly app without ASP.NET Core hosting, so he might not be able to take a server-side strategy such as you suggested.

Hi @fingers10,

@Alerinos do you mean MainLayout.razor in blazor wasm?

I think the answer is no, and he may not want to mean that.
I guess the "_Layout" that he told us is an ASP.NET Core Razor Pages that run on server-side .NET processes. It is contained in Blazor Server or ASP.NET Core Host for Blazor WebAssembly.

@Alerinos
Copy link

Alerinos commented Oct 24, 2022

I understand, in that case you have to write JS code and then execute it in blazor.

Example:

JS.InvokeVoidc("loadJS", "url.js")

https://learn.microsoft.com/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-6.0

JS:
https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants