Skip to content

Blazor - Inject script via C# during build #11325

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

Closed
VR-Architect opened this issue Jun 18, 2019 · 4 comments
Closed

Blazor - Inject script via C# during build #11325

VR-Architect opened this issue Jun 18, 2019 · 4 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one

Comments

@VR-Architect
Copy link

Please consider adding a feature where we can use C# in a razor page to inject javascript code when the user is on the page, then it removes the javascript code when they leave the page. Ideally, this inject would create the required code to handle this during the build.

Currently, I am putting all of my custom javascript on the index.html page so it can get quite long and I lose track of why a method was added in support of some razor page. I am using the Blazor ASP Hosted framework.

Thanks

@Mercurial
Copy link

Mercurial commented Jun 18, 2019

Couldn't you do it yourself in each page / component?

when a component inits

render a <script> element

and unload it later when the component is disposed maybe via JS Interop

@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Jun 18, 2019
@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us, @VR-Architect.
@SteveSandersonMS what do you think about this? Do you think Sections work would cover this?

@mkArtakMSFT mkArtakMSFT added the enhancement This issue represents an ask for new feature or an enhancement to an existing one label Jun 18, 2019
@SteveSandersonMS
Copy link
Member

it removes the javascript code when they leave the page

Unfortunately, unloading is not possible. A <script> tag is just a way of executing some JS within the context of the document. You can't "un-execute" it later.

For now I'd recommend using JS interop as a way to inject extra <script> tags into your document when a component loads. I'm sure it's possible to wrap this in a nice API to make it easy to reuse.

I'm not sure whether we'd make a built-in feature of this kind in the long run. In most cases I'd expect people to want to build complex JS assets using something like Webpack, which contains its own lazy-loading mechanism.

@mkArtakMSFT I suggest backlog or close.

@mkArtakMSFT
Copy link
Contributor

Thanks @SteveSandersonMS.
Closing for now. If we hear more feedback in this area in the future, we'll reconsider.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

No branches or pull requests

4 participants