17 | An unhandled error has occurred.
18 | Reload
19 | 🗙
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/BlazorDynamicScriptLoad/wwwroot/css/open-iconic/ICON-LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Waybury
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
--------------------------------------------------------------------------------
/BlazorDynamicScriptLoad.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.29512.175
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorDynamicScriptLoad", "BlazorDynamicScriptLoad\BlazorDynamicScriptLoad.csproj", "{DE4ACB7C-4CB8-4735-90EF-E3D968A07F23}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {DE4ACB7C-4CB8-4735-90EF-E3D968A07F23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {DE4ACB7C-4CB8-4735-90EF-E3D968A07F23}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {DE4ACB7C-4CB8-4735-90EF-E3D968A07F23}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {DE4ACB7C-4CB8-4735-90EF-E3D968A07F23}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {3077F92A-88D2-4938-A5F1-1E33A1961B2F}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/BlazorDynamicScriptLoad/Shared/NavMenu.razor:
--------------------------------------------------------------------------------
1 |
7 | Sample written as demo in answer to [StackOverflow query](
8 | https://stackoverflow.com/questions/58976579/blazor-server-load-js-scripts-only-on-certain-pages-not-on-all).
9 |
10 |
11 |
12 | The index.html page includes a reference to the scriptLoader.js library, but does not load any
13 | other scripts. This page uses JQuery to change the text of the H1 tag as an example. It loads the JQuery library and
14 | then the myJQueryTest.js interop library, and then invokes the setH1 method on it.
15 |
16 | @code
17 | {
18 | protected override async Task OnAfterRenderAsync(bool firstRender)
19 | {
20 | // invoke script loader
21 | Console.WriteLine("Loading jQuery");
22 | // note the scripts will only load once
23 | await jsRuntime.InvokeVoidAsync("loadScript", "https://code.jquery.com/jquery-3.4.1.js");
24 | await jsRuntime.InvokeVoidAsync("loadScript", "js/myJQueryTest.js");
25 |
26 | Console.WriteLine("Invoking jQuery");
27 |
28 | await jsRuntime.InvokeVoidAsync("setH1", "Hello world!");
29 |
30 | Console.WriteLine("Invoked JQuery");
31 |
32 | await base.OnAfterRenderAsync(firstRender);
33 | }
34 | }
--------------------------------------------------------------------------------
/BlazorDynamicScriptLoad/wwwroot/js/scriptLoader.js:
--------------------------------------------------------------------------------
1 | // loadScript: returns a promise that completes when the script loads
2 | window.loadScript = function (scriptPath) {
3 | // check list - if already loaded we can ignore
4 | if (loaded[scriptPath]) {
5 | console.log(scriptPath + " already loaded");
6 | // return 'empty' promise
7 | return new Promise(function (resolve, reject) {
8 | resolve();
9 | });
10 | }
11 |
12 | return new Promise(function (resolve, reject) {
13 | // create JS library script element
14 | var script = document.createElement("script");
15 | script.src = scriptPath;
16 | script.type = "text/javascript";
17 | console.log(scriptPath + " created");
18 |
19 | // flag as loading/loaded
20 | loaded[scriptPath] = true;
21 |
22 | // if the script returns okay, return resolve
23 | script.onload = function () {
24 | console.log(scriptPath + " loaded ok");
25 | resolve(scriptPath);
26 | };
27 |
28 | // if it fails, return reject
29 | script.onerror = function () {
30 | console.log(scriptPath + " load failed");
31 | reject(scriptPath);
32 | }
33 |
34 | // scripts will load at end of body
35 | document["body"].appendChild(script);
36 | });
37 | }
38 | // store list of what scripts we've loaded
39 | loaded = [];
40 |
--------------------------------------------------------------------------------
/BlazorDynamicScriptLoad/Pages/FetchData.razor:
--------------------------------------------------------------------------------
1 | @page "/fetchdata"
2 | @inject HttpClient Http
3 |
4 |
Weather forecast
5 |
6 |
This component demonstrates fetching data from the server.
35 | }
36 |
37 | @code {
38 | private WeatherForecast[] forecasts;
39 |
40 | protected override async Task OnInitializedAsync()
41 | {
42 | forecasts = await Http.GetJsonAsync("sample-data/weather.json");
43 | }
44 |
45 | public class WeatherForecast
46 | {
47 | public DateTime Date { get; set; }
48 |
49 | public int TemperatureC { get; set; }
50 |
51 | public string Summary { get; set; }
52 |
53 | public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Blazor Dynamic Script Load
2 |
3 | Demonstrates dynamic loading of a JavaScript library in Blazor
4 |
5 | As a demo for a [StackOverflow question](https://stackoverflow.com/questions/58976579/blazor-server-load-js-scripts-only-on-certain-pages-not-on-all/58979941)
6 | I wrote this simple demo app that will load JQuery and a local JS interop library dynamically.
7 |
8 | Often examples of using JavaScript libraries in Blazor add the library to the `_Host.cshtml` or the `index.html` page. This
9 | is fine if most pages are going to use the library, e.g. Bootstrap, but some libraries might only be used on infrequent occassions or
10 | by a small percentage of users. Dynamic loading is a better approach in this case.
11 |
12 | To achieve this the demo has a single script reference in `index.html`:
13 | ```html
14 |
15 |
16 | ```
17 | When a page or component needs to load a specific library, it injects the `IJSRuntime` and invokes `loadScript`. Note this returns
18 | a promise since the script loads asynchronously, so we `await` this in the Blazor code.
19 |
20 | In the example below, we load JQuery and then a simple interop library that sets the H1 text using JQuery.
21 | ```
22 | @page "/"
23 | @inject IJSRuntime jsRuntime
24 |
25 |
Blazor Dynamic Script Load
26 |
27 |
..content removed...
28 | @code
29 | {
30 | protected override async Task OnAfterRenderAsync(bool firstRender)
31 | {
32 | // invoke script loader
33 | Console.WriteLine("Loading jQuery");
34 | // note the scripts will only load once
35 | await jsRuntime.InvokeVoidAsync("loadScript", "https://code.jquery.com/jquery-3.4.1.js");
36 | await jsRuntime.InvokeVoidAsync("loadScript", "js/myJQueryTest.js");
37 |
38 | Console.WriteLine("Invoking jQuery");
39 |
40 | await jsRuntime.InvokeVoidAsync("setH1", "Hello world!");
41 |
42 | Console.WriteLine("Invoked JQuery");
43 |
44 | await base.OnAfterRenderAsync(firstRender);
45 | }
46 | }
47 | ```
48 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/BlazorDynamicScriptLoad/wwwroot/css/open-iconic/README.md:
--------------------------------------------------------------------------------
1 | [Open Iconic v1.1.1](http://useiconic.com/open)
2 | ===========
3 |
4 | ### Open Iconic is the open source sibling of [Iconic](http://useiconic.com). It is a hyper-legible collection of 223 icons with a tiny footprint—ready to use with Bootstrap and Foundation. [View the collection](http://useiconic.com/open#icons)
5 |
6 |
7 |
8 | ## What's in Open Iconic?
9 |
10 | * 223 icons designed to be legible down to 8 pixels
11 | * Super-light SVG files - 61.8 for the entire set
12 | * SVG sprite—the modern replacement for icon fonts
13 | * Webfont (EOT, OTF, SVG, TTF, WOFF), PNG and WebP formats
14 | * Webfont stylesheets (including versions for Bootstrap and Foundation) in CSS, LESS, SCSS and Stylus formats
15 | * PNG and WebP raster images in 8px, 16px, 24px, 32px, 48px and 64px.
16 |
17 |
18 | ## Getting Started
19 |
20 | #### For code samples and everything else you need to get started with Open Iconic, check out our [Icons](http://useiconic.com/open#icons) and [Reference](http://useiconic.com/open#reference) sections.
21 |
22 | ### General Usage
23 |
24 | #### Using Open Iconic's SVGs
25 |
26 | We like SVGs and we think they're the way to display icons on the web. Since Open Iconic are just basic SVGs, we suggest you display them like you would any other image (don't forget the `alt` attribute).
27 |
28 | ```
29 |
30 | ```
31 |
32 | #### Using Open Iconic's SVG Sprite
33 |
34 | Open Iconic also comes in a SVG sprite which allows you to display all the icons in the set with a single request. It's like an icon font, without being a hack.
35 |
36 | Adding an icon from an SVG sprite is a little different than what you're used to, but it's still a piece of cake. *Tip: To make your icons easily style able, we suggest adding a general class to the* `