The details you provide will be handled in accordance with our stated Privacy Policy.
This article provides an explanation of the HtmlDocumentLinkInjector class included the CalzadaMedia.Web common library. For reference, an example based upon working code is also provided.
The HtmlDocumentLinkInjector class provides functionality to insert HTML Hyperlinks into an HTML document based upon matching texts. The principle use of the HtmlDocumentLinkInjector is to automatically insert links into multiple HTML documents without the need to actually specify the link within the document itself.
The HtmlDocumentLinkInjector simplifies overall link management. There is no need to manually add links to every HTML document. With the HtmlDocumentLinkInjector you define the link once and then run the HTML content through it.
For example, you may wish to automatically link to your website's Privacy Policy every time the words Privacy Policy are used (see Code Example below). The HtmlDocumentLinkInjector can search through HTML document for all occurences of Privacy Policy and then insert a Hyperlink around this text to point towards the Url of your Privacy Policy.
The HtmlDocumentLinkInjector only inserts links if none already exist. If any matching text is found within an existing link, it is not processed.
There are there parameters for each potential match: the match text, the Url and the tooltip.
Using the above example, the following code will add a hyperlink around every instance of "Privacy Policy" found within the value of htmlText.
Dim linkInjector As New CalzadaMedia.Web.UI.HtmlDocumentHyperlinkInjector linkInjector.AddLink("http://mydomain.com/privacy-policy.html", "Our Privacy Policy") htmlText = linkInjector.InjectLinks(htmlText) linkInjector.Dispose()
Links may be added individually using the AddLink method or in multiple using the AddLinks method. With AddLinks, a System.Data.DataTable of link values must be supplied along with the relevant column names.
AddLink
AddLinks
In the code example below, the DataTable mylinksDataTable contains the columns link_matchtext, link_url, and link_tooltip respectively contain the values of the match text, the Url to insert and any tooltip to use.
mylinksDataTable
link_matchtext
link_url
link_tooltip
Dim linkInjector As New CalzadaMedia.Web.UI.HtmlDocumentHyperlinkInjector linkInjector.AddLinks(myLinksDataTable, "link_matchtext", "link_url", "link_tooltip") htmlText = linkInjector.InjectLinks(htmlText) linkInjector.Dispose()
Additional Hyperlink attributes like class, rel and target may be specified using the LinkAttributes property.
LinkAttributes
Please note: The attributes id or title may not be specified. They will be ignored if they are specified.
In the code example below, the target attribute is specified.
target
Dim linkInjector As New CalzadaMedia.Web.UI.HtmlDocumentHyperlinkInjector linkInjector.AddLink("http://mydomain.com/privacy-policy.html", "Our Privacy Policy") linkInjector.Attributes.Add("target", "_blank") htmlText = linkInjector.InjectLinks(htmlText) linkInjector.Dispose()
It is strongly recommended the Dispose() method is called once you have finished with the HtmlDocumentLinkInjector
Dispose()