Title

jQuery do something if referrer URL contains ‘string’

description

Check with JS if the URL that the visitor came from when landing contains a certain string

Published 2022-05-24

Modified 2022-08-19

content

				
					jQuery(document).ready(function($){
    var arrivedFrom = document.referrer;
    var urlContains = "home/index.html";

    if (arrivedFrom.indexOf(urlContains) > -1)
        // Bingo! Do something
        $('#xyz').css('background-color','green');
    else 
        // Nope! Do nothing or do something else
        $('#xyz').css('background-color','red');
});