Rather than just using window.location
for your url, try using:
var url = window.location.pathname;
If your current url is www.mywebsite.com/index.php?page=2
the above should output /index.php
.
You can use url.replace('/', '')
to get rid of that first slash.
EDIT:
Since your pathname may have multiple parts, such as /something/index.php
we need to deal with that. Another way of doing it is:
var urlpath = window.location.pathname;
In your code you have:
$('ul.nav a[href="' + url + '"]').parent().addClass('active');
You can change the selector to:
$('ul.nav a[href="' + urlpath.split('/').pop() + '"]').parent().//...etc
This is doing the splitting and isolating the index.php
portion in one go. index.php
will of course be contact.php
or whatever.php
on other pages.
Try this JSFiddle I just created, showing the above line in action: http://jsfiddle.net/N9SHq/