Skip to main content

JavaScript function to get the referring URL of the current document.

/**
 * Gets the referring URL of the current document.
 *
 * @return {String} The referring URL, or empty if not found.
 */
function getReferrer() {
    var referrer = '';

    try {
        referrer = window.top.document.referrer;
    }
    catch (e) {
        if (window.parent) {
            try {
                referrer = window.parent.document.referrer;
            }
            catch (e2) {
                referrer = '';
            }
        }
    }

    if (referrer === '') {
        referrer = document.referrer;
    }

    return referrer;
}