Adam
Adam

Reputation: 20922

How to check whether url contains "main.php"

I need to be able to know if the URL contains "main.php". How can I do this in jQuery?

I tried this

$(function() {
    if ( document.location.href.indexOf('main.php') > -1 ) {
        alert('hi');
    }
});

The full url would be www.flirtwithme.co/main.php

What's the most efficent way to capture the main.php?

Also I'm using hash like this

http://localhost/flirtwithme.co/main.php#profile

with $(window).bind('hashchange')

So how can I capture main.php when it doesn't have a hash value. This is the real question I'm after, as I have hash changing working. I want the URL alone (main.php) to be controllable.

Upvotes: 0

Views: 208

Answers (2)

Petah
Petah

Reputation: 46060

Try:

var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
if (filename == 'main.php' && window.location.hash == '') // do stuff

Upvotes: 2

alex
alex

Reputation: 490423

To get the last segment...

var lastPathSegment = window.location.pathname.split('/').pop();

So how can I capture main.php when it doesn't have a hash value. This is the real question I'm after, as I have hash changing working. I want the URL alone (main.php) to be controllable.

Look into the new methods on the history object, such as history.pushState().

Upvotes: 2

Related Questions