Issue with a javascript code.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I am using the following code to get the name of the previous page that the user visited:
Code:
    Private Function GetPreviousURL() As String

        Dim script As String = Nothing

        script &= "<script language=JavaScript id='previous'>" & vbCrLf
        script &= "document.referrer;" & vbCrLf
        script &= "</script>" & vbCrLf

        'Verify script has not already been registered.
        If Not ClientScript.IsStartupScriptRegistered("previous") Then
            'Register the script.
            ClientScript.RegisterStartupScript(Page.GetType, "previous", script)
        End If

    End Function
[code]

My only problem is that I am unsure as to how to get the value generated by document.referrer back to a vb.net variable.  Any suggestions?

Mike55.
 
Request.UrlReferrer

There is no need to use client-side scripts for this. You can use just:

Visual Basic:
Private Function GetPreviousURL() As String
    Return Request.UrlReferrer
End Function

Good luck :cool:
 
MrPaul said:
There is no need to use client-side scripts for this. You can use just:

Visual Basic:
Private Function GetPreviousURL() As String
    Return Request.UrlReferrer
End Function

Good luck :cool:

Thanks for the reply MrPaul.

Would love to be able to use that simple command, however, it appears not to work on all occasions. When I call it, it throws an exception, and all hell breaks loose.

Mike55.
 
Ok, the code:
Code:
Request.UrlReferrer.AbsoluteUri
works correctly, if I am moving from page A to B. The problem arises when I use the browser back button to go from page B back to A, or a button that I have added to the page that redirects the user back to page A. I have had a number of problems with the above command in that it does not always have a value, thus causing me problems.

Mike55.
 
You are probably going to encounter the same problems via Javascript that you are encountering from .Net - the Referer will only contain a valid value when you have been referred to the current page (i.e. following a link or submitting a page).
 
Back
Top