Find URLs in string and make as link inside content page using JavaScript in ASP.Net

t01779393200
 
on Aug 26, 2021 04:43 AM
652 Views

Ref: Find URLs in string and make as link using JavaScript in ASP.Net

this code not work my crome webbrowser

please more help

my code is.

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = "I love asp county ilove my http://www.aspforums.net this is very good site of world my http://www.aspforums.net/ im allway visit https://www.aspforums.net/Threads/";
    }
}

 

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<script type="text/javascript">
    function GenerateLinks(message) {
        var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
        return message.replace(urlRegex, function (url) {
            var hyperlink = url;
            if (!hyperlink.match('^https?:\/\/')) {
                hyperlink = 'http://' + hyperlink;
            }
            return '<a href="' + hyperlink + '" target="_blank" >' + url + '</a>'
        });
    }
    window.onload = function () {
        document.getElementById('Label1').innerHTML = GenerateLinks(document.getElementById('Label1').innerHTML);
    };
</script>
</asp:Content>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Aug 26, 2021 04:51 AM

Hi t01779393200,

Use below code.

HTML

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<script type="text/javascript">
    function GenerateLinks(message) {
        var urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
        return message.replace(urlRegex, function (url) {
            var hyperlink = url;
            if (!hyperlink.match('^https?:\/\/')) {
                hyperlink = 'http://' + hyperlink;
            }
            return '<a href="' + hyperlink + '" target="_blank" >' + url + '</a>'
        });
    }
    window.onload = function () {
        document.getElementById('<%=Label1.ClientID%>').innerHTML = GenerateLinks(document.getElementById('<%=Label1.ClientID%>').innerHTML);
    };
</script>

Code

protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = "I love asp county ilove my http://www.aspforums.net this is very good site of world my http://www.aspforums.net/ im allway visit https://www.aspforums.net/Threads/";
}

Screenshot