cliveportman/code

Check URL using jQuery

Ages ago I commented somewhere and left a link to my site within the comment which included the ID of a div called (http://cliveportman.co.uk/#nameofadiv). Since then the div no longer exists, but I wanted to offer those visitors the download still.

You can’t do anything with PHP because anything with a # isn’t passed to the server, so I had to resort to some Javascript and search the url:

$(document).ready(function() {

    $url = $(location).attr(‘href’);
    $string = “nameofadiv”
    if ($url.search($string) == 24) {
        
    }
});

Where 24 is the position of the string in the url.