C#: Return value of a href

Asked

Viewed 256 times

0

How do I get the address/URL of a tag

 < a href="js:redir(2)">
Through an HTML source?

NOTE: I tried to use Assembly mshtml, but it also didn’t work!

2 answers

1


You can use this example to implement in your project, regular expression is used.

C#

private static void DumpHRefs(string inputString)
{
    Match m;
    string HRefPattern = "href\\s*=\\s*(?:[\"'](?<1>[^\"']*)[\"']|(?<1>\\S+))";

    try
    {
         m = Regex.Match(inputString, HRefPattern,
         RegexOptions.IgnoreCase | RegexOptions.Compiled,
         TimeSpan.FromSeconds(1));
         while (m.Success)
         {
             Console.WriteLine(m.Groups[1]);
             m = m.NextMatch();
         }
    }
    catch (RegexMatchTimeoutException)
    {
         Console.WriteLine("The matching operation timed out.");
    }
}

And to use the function just send the code HTML as a parameter.

C#

string inputString = "My favorite web sites include:</P>" +
                        "<A HREF=\"http://msdn2.microsoft.com\">" +
                        "MSDN Home Page</A></P>" +
                        "<A HREF=\"http://www.microsoft.com\">" +
                        "Microsoft Corporation Home Page</A></P>" +
                        "<A HREF=\"http://blogs.msdn.com/bclteam\">" +
                        ".NET Base Class Library blog</A></P>";

DumpHRefs(inputString);

Upshot

http://msdn2.microsoft.com

http://www.microsoft.com

http://blogs.msdn.com/bclteam

The project was built using console, just adapt the function to your project.

NOTE: It may be necessary to include the reference below

using System.Text.RegularExpressions;

-1

If I understand what you need, you can do it this way:

<htmL>
<body>
A url completa da página atual é:
<script>
document.write(document.URL);
</script>

</body>
</html>

document.write(window.location.href)

Other forms:

document.write(window.location.protocol) 
document.write(window.location.host) 
document.write(window.location.pathname) 

The code above will display:

"http"
"teste.com.br"
"login/index.html"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.