Is it possible to use a specific .js
file just inside a particular razor page when using blazor and .Net 5?
Just to provide some context, I created a simple script named MyJavaScript.js
:
function myfunction()
{
alert("Hello");
}
I then added this script to _host.cshtml:
<script src="/MyJavaScript.js"></script>
Currently, whenever I navigate to any razor page in my web app (e.g., localhost:1212
, localhost:1212/counter
), the .js
file is loaded on the first visit and accessible across all razor pages. However, I am interested in restricting the usage of this .js
file to just one specific razor page (mypage.razor
). Is there a way to achieve this?
In simpler terms, I want only localhost:1212/mypage
to display the alert and have access to myfunction
, rather than making it available across all razor pages. Where should I place the MyJavaScript.js
file to accomplish this?
Update 1: It seems that placing the <script></script>
tag inside a component is not supported.