简体   繁体   中英

Get URL of referring page and pass it to a hidden form field

I need to know how to get the url of the referring page of my website. When a user clicks a link to a contact form on my website (from another page on my website) how can I get the url of that page? I have tried document.referrer but it only shows the domain name of my website, I need to see what page a visitor was viewing when they clicked on the link to the contact form. My site is static html, css and javascript.

Also tried setting a cookie, but it only shows the page where the contact form is actually located, for example: www.mysite.com/contact-us.html . What I need to know is what page the visitor was viewing when they clicked on the link that brought them to the form on the contact page.

My objective is to pass the url into a hidden form field, so when submitted, I can tell which page of my website the user was viewing when they clicked on the link to the contact form. Then, if they ask a question about a specific product, I will know which page they were on when they viewed the product. Note: I have 100+ product pages that link to the contact form.

If you have a master page used by all the product pages, you can save the current url as session in master page when the product page was loaded. Then you can retrieve the session url when contact page was loaded. This should able do the work.

You can use the HttpRequest.UrlReferrer property.

Gets information about the URL of the client's previous request that linked to the current URL.

public partial class Contact : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Uri urlReferrer = Request.UrlReferrer;
        if (urlReferrer != null)
        {
            hiddenFieldUrlReferrer.Value = urlReferrer.ToString();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM