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

Haneen Al-fakhry 10 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?

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Blazor
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    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) 77,686 Reputation points Volunteer Moderator
    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.