patricko
patricko

Reputation: 831

jquery - check for part of a url

I'm using jquery, and I need to check the URL to see if the user is looking at tagged/categorized content.

I need to check the URL to do so. Before, I was doing something like this to check to see what page the user was on...

var loc = window.location; 
var pageOne = 'http://design.optimus.com/projects';
if(loc == pageOne) { 
//do stuff
}

But these URLs never changed, so it was easy for me to check if they were on a single URL or not.

However, the new urls are going to have different endings depending on the tag the user searches for. For example,

http://optidesign.squarespace.com/projects/tag/design
http://optidesign.squarespace.com/projects/tag/production
http://optidesign.squarespace.com/projects/tag/animation

So I want to use sort of the same method as the first part, but I just want to check for the beginning of the URL:

http://optidesign.squarespace.com/projects/tag/

How do i go about doing this?

Upvotes: 2

Views: 1689

Answers (5)

dazbradbury
dazbradbury

Reputation: 5749

If you have access to the page contents, you could simply add a variable to pass into your script stating which page you are on.

Assuming the URL is the only data you have to go off, you can use the indexOf to see if that part of the URL is present or not.

var loc = window.location.toString(); 
var pageOne = 'http://design.optimus.com/projects/tag/';
if(loc.indexOf(pageOne) >= 0) { 
//do stuff
}

In order to be future-proof in case you change domain or switch to HTTPS, you could alternatively use the following:

var loc = window.location.pathname; 
var pageOne = '/projects/tag/';
if(loc.indexOf(pageOne) >= 0) { 
//do stuff
}

I have put an example on JSFiddle you can play with.

Upvotes: 1

adeneo
adeneo

Reputation: 318342

To just check if 'tag' is present:

if (window.location.match(/tag/)!==null) {
    //do something
}

To remove the last part of any URL:

var url = window.location.split('/');
    url.pop();
    url = url.join().replace(/,/g, '/')+'/';

Upvotes: 0

David Thomas
David Thomas

Reputation: 253486

If you want to know what page the user is on, assuming that's the 'directory' following the tag, then I'd suggest:

if (url.indexOf('tag')) {
    var temp = url.split('/tag/')[1];
    var curPage = temp.substring(0,temp.indexOf('/'));
    return curPage;
}

JS Fiddle demo.

Upvotes: 0

Salvatorelab
Salvatorelab

Reputation: 11873

You can use the window.location.pathname instead, cause the hostname (stackoverflow.com for example) is always the same. So, window.location.pathname will give you the rest of the URL. In this example, will give you: "/questions/9706497/jquery-check-for-part-of-a-url"

Now, if you want to check the first two segments, you can split the string:

window.location.pathname.split("/");

That will give you:

["", "questions", "9706497", "jquery-check-for-part-of-a-url"]

And now check the index you need, for example:

parts = window.location.pathname.split("/");
if (parts[1] == "projects") {
    alert("Projects!!");
}

Upvotes: 0

Rob W
Rob W

Reputation: 349232

The indexOf method can be used for this purpose:

if (location.href.indexOf('http://optidesign.squarespace.com/projects/tag/') === 0) {
    // Well...
}

I can imagine that you test the pages on localhost, or at a different protocol. The following solution might be more useful:

if (location.pathname.indexOf('/projects/tag/') === 0) {
    // Well...
}

Upvotes: 4

Related Questions