2

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?

patricko
  • 831
  • 4
  • 16
  • 34

5 Answers5

4

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...
}
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Is the === 0 the same as returning true or false? – patricko Mar 14 '12 at 17:05
  • @patricko `=== 0` is an [identity operator](http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use). It returns `true` when the value and type at both sides are equal, `false` otherwise. – Rob W Mar 14 '12 at 17:06
  • Also, if it's of any help to you, I have nice little [plugin](http://spyk3lc.blogspot.com/2012/03/jquery-myurl-extension-how-to-get-that.html) that makes any and all calls to ur sites url **EASY**. See working [fiddle here](http://jsfiddle.net/SpYk3/2ZuTe/). Examples of how to use it are on my [blog](http://spyk3lc.blogspot.com/2012/03/jquery-myurl-extension-how-to-get-that.html) – SpYk3HH Mar 14 '12 at 17:29
1

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.

dazbradbury
  • 5,729
  • 5
  • 34
  • 38
0

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.

David Thomas
  • 249,100
  • 51
  • 377
  • 410
0

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!!");
}
Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
0

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, '/')+'/';
adeneo
  • 312,895
  • 29
  • 395
  • 388