How can I tell if a <script> tag with a given src attribute is present on an ASP.net page?

As in the title, I'm trying to figure out if I need to include a script library that my ASP.net UserControl should use. I don't want to include it multiple times per page, but I want my control to be able to be used multiple times on the same page.

How can I check if a given tag is present in the control code of my control <script/>

?

This is .Net 2.0, not LINQ.

+2


a source to share


4 answers


If !Page.ClientScript.IsClientScriptIncludeRegistered("jQuery")
    Page.ClientScript.RegisterClientScriptInclude("jQuery", "/scripts/jquery.js");

      

or if you need a script block and want to include a control type:



if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "myScript"))
      Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myScript"
                                       , "<script>alert('xx');</script>", false);

      

+2


a source


This might be a little overkill for your use, but I use it to search for existing CSS and JS before adding to the page:

private static bool HeaderLinkExists(Page page, string path)
{
    path = path.ToLowerInvariant();
    foreach (Control c in page.Header.Controls)
    {
        if (c is HtmlLink)
        {
            // stylesheet (or other links), check href
            HtmlLink link = (HtmlLink)c;
            if (link.Href.ToLowerInvariant().Contains(path))
            {
                return true;
            }
        }
        else if (c is HtmlGenericControl)
        {
            // any generic html tag, check for src or href
            HtmlGenericControl hgc = (HtmlGenericControl)c;
            if ((!string.IsNullOrEmpty(hgc.Attributes["src"]) && hgc.Attributes["src"].ToLowerInvariant().Contains(path)) || (!string.IsNullOrEmpty(hgc.Attributes["href"]) && hgc.Attributes["href"].ToLowerInvariant().Contains(path)))
            {
                return true;
            }
        }
        else if (c is LiteralControl)
        {
            // scripts or other html literal controls, use regex to look for src or hrefs and check each one
            LiteralControl lit = (LiteralControl)c;
            if (MatchLiteralText(lit.Text, path))
            {
                return true;
            }
        }
        else if (c is Literal)
        {
            // similar to above, use regex to look for src or hrefs and check each one
            Literal lit = (Literal)c;
            if (MatchLiteralText(lit.Text, path))
            {
                return true;
            }
        }
    }
    return false;
}

private static readonly Regex linkMatcher = new Regex(@"(?:src|href)\s*=\s*([""']?)(?<LinkValue>[^\1]+?)[\1>]", RegexOptions.Compiled);

private static bool MatchLiteralText(string text, string path)
{
    if (!string.IsNullOrEmpty(text))
    {
        text = text.ToLowerInvariant()
        foreach (Match m in linkMatcher.Matches(text))
        {
            if (m.Groups["LinkValue"].Value.Contains(path))
            {
                return true;
            }
        }
    }
    return false;
}


// usage:
if (!HeaderLinkExists(page, "/css/controlstyles.css"))
{
    HtmlHeadUtility.RegisterStylesheetInHeader(page, "~/css/controlstyles.css");
}
if (!HeaderLinkExists(page, "/js/controlscript.js"))
{
    HtmlHeadUtility.RegisterClientScriptIncludeInHeader(page, "~/js/controlscript.js");
}

      



It works great if you are sure LINK

or SCRIPT

guaranteed to be in HEAD

.

+1


a source


No no.

Instead, you should maintain a set of tags <script>

that were already included and update it whenever you add a script. (For example a [ThreadStatic] static List<String>

)

0


a source


I would suggest rethinking your problem / solution. It seems that what you are trying to do will pair your user control and the pages that use it. This will require a link to the page that is script in order to use the control.

Is there any reason why you can't just put a script tag in your custom control so that pages consuming your user control don't have to know that they need to include a specific script tag in order to work?

I tend to think that if you just want to use the control, you should just use it.

0


a source







All Articles