Forum Discussion

iftomkins's avatar
10 years ago

Get URL of the previous page in the page initialization tab

I'm trying to get the URL of the previous page the user was on. Any way to do this works. I could possibly add a "return" parameter onto the URL before the user leaves the first page, in which case, I'd like to know how to get the value of a query variable. Otherwise, accessing the previous URL directly would be ideal. Here's the basic idea: 

 

<#if page.name?lower_case == "postpage" && page.interactionStyle == 'idea'>

 

<#-- IF PREVIOUS URL contains /idb-p/features -->

    DO NOT REDIRECT

<#-- ELSE -->

    <#-- Redirect -->

    ${http.response.setRedirectUrl("/t5/Feature-Requests/idb-p/features")}

<# END IF --> 


</#if>

  • Hi iftomkins 

     

    what about accessing the HTTP Referrer from the HTTP request context object

     

    ${http.request.referrer}

     This should do the trick I think

     

    Thanks,

  • PaoloT's avatar
    PaoloT
    Lithium Alumni (Retired)

    Hi iftomkins 

     

    what about accessing the HTTP Referrer from the HTTP request context object

     

    ${http.request.referrer}

     This should do the trick I think

     

    Thanks,

    • iftomkins's avatar
      iftomkins
      Maven

       

      Thanks, PaoloT ! That did work. I'm stuck on one thing though. I think it's erroring out when there is no http.request.referrer.

       

      The below code runs on page initialization on the page: /t5/forums/postpage/board-id/features

       

      When I go from /t5/Feature-Requests/idb-p/features and click new post, I go to /t5/forums/postpage/board-id/features, correctly it does not load, and I am successfully redirected via this redirect:

      ${http.response.setRedirectUrl("/t5/custom/page/page-id/AcceptIdeasTerms?postlocation=/t5/forums/postpage/board-id/" + postTo)}

       

      However, when I paste the URL of the post page in the browse (/t5/forums/postpage/board-id/features) , I am not redirected, and the post page loads. 

       

      According to the logic in the code below, I think the user should be directed to /t5/Feature-Requests/idb-p/features if http.request.referrer does not exist, or if it exists, but does bnot contain ("/idb-p/").

       

      My guess is that it's erroring out, and therefore not redirecting, because of the handling of an unexpected value for prevUrl (http.request.referrer). Any thoughts on how this might be resolved? Again, when prevUrl does contain "/idb-p/" then that part of the code works great.

      <#if terms_accepted != 'true'>

          <#assign prevUrl = http.request.referrer />
          <#if prevUrl??>
              <#if prevUrl?contains("/idb-p/")>
                  <#assign urlSections = prevUrl?split("/") />
                  <#assign postTo = urlSections[urlSections?size - 1] />
                  ${http.response.setRedirectUrl("/t5/custom/page/page-id/AcceptIdeasTerms?postlocation=/t5/forums/postpage/board-id/" + postTo)}
              <#else>
                  ${http.response.setRedirectUrl("/t5/Feature-Requests/idb-p/features")}
              </#if>
          <#else>
              ${http.response.setRedirectUrl("/t5/Feature-Requests/idb-p/features")}
          </#if>

      </#if>

      • iftomkins's avatar
        iftomkins
        Maven

        Ok! Answered my own question. I needed to test if http.request.referrer exists before assigning its value to a variable. Here's the updated code:

         

         <#if terms_accepted != 'true'>

        <#if http.request.referrer?? >
        <#assign prevUrl = http.request.referrer />
        <#if prevUrl?contains("/idb-p/")>
        <#assign urlSections = prevUrl?split("/") />
        <#assign postTo = urlSections[urlSections?size - 1] />
        ${http.response.setRedirectUrl("/t5/custom/page/page-id/AcceptIdeasTerms?postlocation=/t5/forums/postpage/board-id/" + postTo)}
        <#else>
        ${http.response.setRedirectUrl("/t5/Feature-Requests/idb-p/features")}
        </#if>
        <#else>
        ${http.response.setRedirectUrl("/t5/Feature-Requests/idb-p/features")}
        </#if>

        </#if>