To check if a specific script has already been loaded in C#, you typically need to interact with the HTML page using a library like HtmlAgilityPack or CsQuery. These libraries allow you to parse the HTML content of a page and query for specific elements, including <script>
tags.
Here’s an example of how you can use HtmlAgilityPack to check if a script with a particular source (e.g., src
) has been loaded:
using HtmlAgilityPack;
// Assuming you have the HTML content of the page stored in a string variable called "htmlContent"
// and the script source you want to check is stored in a string variable called "scriptSrc"
var doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
var scriptTags = doc.DocumentNode.SelectNodes("//script[@src]");
if (scriptTags != null)
{
foreach (var scriptTag in scriptTags)
{
var srcAttribute = scriptTag.GetAttributeValue("src", "");
if (srcAttribute.Contains(scriptSrc))
{
// The script with the specified source has been loaded
// Do something here
break;
}
}
}
In this example, we use HtmlAgilityPack to parse the HTML content and select all <script>
tags that have a src
attribute. Then, we iterate through these script tags to check if the src
attribute contains the specified script source (scriptSrc
). If it does, it means the script has been loaded.
Keep in mind that this approach only checks for the presence of the script tag in the parsed HTML. It doesn’t guarantee that the script has fully loaded or executed, as the actual loading and execution might depend on various factors, such as browser behavior and network conditions.