0

Is there any function in VBScript that returns the absolute URL when we have domain and the relative URL. Suppose I have the domain example.com/ and the relative path home/index.html. When the 2 values are passed to the function, the result returned should be example.com/home/index.html

For example in Java, there's a resolve method which does this job. And suppose we pass the values example.com/ and example.com/home/index.html to the method, the method returns example.com/home/index.html

4
  • Is this VBScript or Classic ASP using VBScript? Because if it's Classic ASP the answer is quite different.
    – user692942
    Commented Jul 6, 2020 at 14:47
  • Am using VBScript.
    – Trooper78
    Commented Jul 6, 2020 at 14:50
  • No, there is nothing built it that will parse or construct a URL for you but there are many examples of functions built to do this.
    – user692942
    Commented Jul 6, 2020 at 14:53
  • Would you be kind and provide me an example link for the same. Am unable to get the right keywords during my google search.
    – Trooper78
    Commented Jul 6, 2020 at 14:57

1 Answer 1

0

If you mean a Decomposition of an URL adress ? here is an example in vbscript :

Option Explicit
Dim adress,result,Title
Title = "Decomposition of an URL"
'Some examples for testing
'adress = "http://www.laltruiste.com:8080/coursasp/sommaire.html#ancre"
'adress = "ftp://ftp.microsoft.com/softlib/index.txt‎"
'adress = "http://www.google.com"
adress = InputBox( "Please input the http or the https address.", " What makes up a Url ?",_
"http://www.laltruiste.com:8080/coursasp/sommaire.html#ancre")
result = Search(trim(adress))
MsgBox Title & " ( Uniform Resource Locator ) ==> URL : " & DblQuote(adress) & vbCrLf & vbCrLf & result,64,Title
'*******************************************************
Function Search(MyString)
    Dim objet,correspondance,collection,pattern
    pattern="^" & _
        "(\w+):\/\/([^/:]+)" & _
        "(:(\d+))?" & _
        "(" & _
            "\/" & _
            "(" & _
                "(" & _
                    "([^/]+)" & _
                    "\/" & _
                ")?" & _
                "(" & _
                    "([^#]+)" & _
                ")?" & _
                "(" & _
                    "(#(\w+)?)?" & _
                ")?" & _
            ")?" & _
        ")?" & _
    "$"
    
    Set objet = New RegExp
    objet.Pattern = Pattern
    objet.IgnoreCase = True
    objet.Global = True
    if objet.test(MyString) then
        Set collection = objet.Execute(MyString)
        Set correspondance = collection(0)
        
        result = "Protocol = " & DblQuote(correspondance.SubMatches(0)) & VbCRLF & vbCrLf _
        & "Domain = " & DblQuote(correspondance.SubMatches(1)) & VbCRLF & vbCrLf _
        & "Port = " & DblQuote(correspondance.SubMatches(3)) & vbCrLf & vbCrLf  _ 
        & "Folder = " & DblQuote(correspondance.SubMatches(7)) & VbCRLF& vbCrLf  _
        & "File = " & DblQuote(correspondance.SubMatches(9)) & VbCRLF& vbCrLf  _ 
        & "Anchor = "& DblQuote(correspondance.SubMatches(12))
        
        Search = result
    else
        Search = MsgBox("no match ===> no result found !",48,Title)
    end if
End Function
'----------------------------------------------------------------
'Function to add double quotes into a variable
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'----------------------------------------------------------------

You can also use PowerShell to do this very easily.

$uri = "http://www.laltruiste.com:8080/coursasp/sommaire.html#ancre"
[system.uri]$uri

And you can get as result like that :

AbsolutePath   : /coursasp/sommaire.html
AbsoluteUri    : http://www.laltruiste.com:8080/coursasp/sommaire.html#ancre
LocalPath      : /coursasp/sommaire.html
Authority      : www.laltruiste.com:8080
HostNameType   : Dns
IsDefaultPort  : False
IsFile         : False
IsLoopback     : False
PathAndQuery   : /coursasp/sommaire.html
Segments       : {/, coursasp/, sommaire.html}
IsUnc          : False
Host           : www.laltruiste.com
Port           : 8080
Query          :
Fragment       : #anchor
Scheme         : http
OriginalString : http://www.laltruiste.com:8080/coursasp/sommaire.html#ancre
DnsSafeHost    : www.laltruiste.com
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.