Previous Page Referral To My Website

时间:2015-11-12 11:43:07

标签: javascript php url referrer

I am trying to return a value depending on which is the previous page the user came from.

I have looked at PHP with $_SERVER['HTTP_REFERER']but this is no longer supported.

I have looked at JavaScript with

function sourcePrevious(){

    if (document.referrer.search('https?://(.*)google.([^/?]*)') === 0) {
        return 'Google';
    } else if (document.referrer.search('https?://(.*)yahoo.([^/?]*)') === 0) {
        return 'Yahoo';
    } else if (document.referrer.search('https?://(.*)bing.([^/?]*)') === 0) {
        return 'Bing';
    }else if (document.referrer.search('https?://(.*)facebook.([^/?]*)') === 0) {
        return 'Facebook';
    }else {
        return 'Other';
    }

}

alert(sourcePrevious());

Which works in part, for Google, Yahoo & Facebook its working but for Bing and Twitter this is not the same case.

Can anyone point me in a direction of a method to do this or let me know how to get the JavaScript function to work for all sources.

Thanks!

1 个答案:

答案 0 :(得分:0)

我解决了TWITTER问题

(document.referrer.search('https?://(.*)t.([^/?]*)') === 0)