how to use script files in blazor project inside index.html?

Haneen Al-fakhry 5 Reputation points
2023-09-25T07:15:58.0533333+00:00

I have a blazor webassemly project , I want to use javascript files , I put them in wwwroot folder and call them in index.html by thos way <script src="/js/bracket.js"></script> I am sure the path is correct but also not working

How to include scripts inside index.html?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,796 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,513 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,555 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,839 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JasonPan - MSFT 5,456 Reputation points Microsoft Vendor
    2023-09-25T11:20:48.1333333+00:00

    Hi @Haneen Al-fakhry,

    I have test it in my local, I know you can't find it in browser developer tools. But the javascript files has been loaded successfully, you can test in browser window.

    If you want check the files in browser developer tools, we need include the js when debugging/deploying. Like below:

    <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
    
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.11" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.11" PrivateAssets="all" />
        <!--// add this line-->
        <Content Include="js/*.js" CopyToOutputDirectory="PreserveNewest" />
      </ItemGroup>
      
    </Project>
    
    
    

    Then we can find all the js files.
    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Jason

    0 comments No comments

  2. Bruce (SqlWork.com) 64,081 Reputation points
    2023-09-25T16:51:24.73+00:00

    you shoudn't have to add the content include for content in the wwwroot folder. but check the deployed wwwroot folder. most likely the url is wrong. as the base href is set in the index.html file, you should use relative.

    <script src="js/bracket.js"></script>

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.