$1.59

how to create back button with different parent location using javascript or Jquery

Ask a question+
1

Hi,

I created a back button where page1 and page2 redirects to page3.
When the back button is clicked on page3, it redirects back to page1 or page2 accordingly. I used the document.referrer, which sometimes returns the page itself instead of the parent location.
That is when clicking the back button on page3, and it redirects to page3.

function GoBackWithRefresh(event) 
{
    if ('referrer' in document) 
    {
        var ref = document.referrer;
        console.log("document referrer_____<BR>"+ref);
        window.location = document.referrer;
        /* OR */
        //location.replace(document.referrer);
    } 
    else 
    { 
        console.log("window history____"+window.history);
        window.history.back(); 
    }
}
Vidya_v5 10
add comment

1 Answer

1

In the function called when click on Back button,

GoBackWithRefresh(event) 
function GoBackWithRefresh(event) 
{
    var referrer =  document.referrer.trim();
    if (!referrer.length) 
    {// referrer is empty 
        window.history.go(-1);
    }
    else 
    { 
        console.log("window history____"+window.history);
        window.history.back(); 
    }
}
PaxObi 10

Thank U! It works.

Vidya_v5
add comment

Your Answer