0

I have this url:

csumb/index.php?page=consultanta 

I try to compare 2 links but this code do the same thing if I change my link and I refresh the page.

var pathname = window.location.pathname;
var a = "csumb/index.php?page=consultanta";

if(pathname == a) {
    $("body").html("rahat");
}
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
  • 2
    Have you tried `console.log(pathname);` to see whats actually in it? I suspect you're missing a slash at the start of the string. – Tom Walters Jan 12 '13 at 10:53
  • 2
    `location.pathname` gives you the *path*, e.g., `/`. `?page=consultanta` is a *query string*. – Jared Farrish Jan 12 '13 at 10:53
  • 1
    Take a look here for the different parts of the URL you have access to in JS: http://stackoverflow.com/questions/6944744/javascript-get-portion-of-url-path – Tom Walters Jan 12 '13 at 10:56

4 Answers4

2

There's multiple parts to location (or window.location, to use the literal reference):

https://packagist.org/search/?search_query%5Bquery%5D=symfony

Using console.log(location) directly into the Chrome console, it gives the following properties (plus some other stuff):

hash: ""
host: "packagist.org"
hostname: "packagist.org"
href: "https://packagist.org/search/?search_query%5Bquery%5D=symfony&page=4"
origin: "https://packagist.org"
pathname: "/search/"
port: ""
protocol: "https:"
search: "?search_query%5Bquery%5D=symfony&page=4"

What you're really after is:

var pathname = location.pathname + location.search;
var a = "/csumb/index.php?page=consultanta";
//       ^ Note the / at the beginning

The filename, if in the URL, will be in location.pathname, so index.php will not need to be added separately either.

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
1

Try this:

var pathname = window.location.pathname;
var search = window.location.search;
var a = "/csumb/index.php?page=consultanta";

if((pathname + search) == a) {
    $("body").html("rahat");
}
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
0

You need to use the === operator and you need to add a "/".

Also you need window.location.search:

var a = "/csumb/index.php?page=consultanta";
var search = window.location.search;
var pathname = window.location.pathname;

if(pathname + search === a) {
    $("body").html("rahat");
}
asgoth
  • 35,552
  • 12
  • 89
  • 98
0

It's not jQuery, please edit the title to javascript. the jQuery part is not used in if statement. window.location.pathname returns something like /csumb/index.php without parameters. so if you like to check the pathname with other parameters, you will need to do this :

var path = window.location.pathname + window.location.search; 
Mohsen Rabieai
  • 244
  • 3
  • 17
  • I tried tthis: var pathname = window.location.pathname; var search = window.location.search; var a = "http://localhost/csumb/index.php?page=consultanta"; if((pathname + search) == a) { $("#outPopUp").fadeIn(3000); } else { $("#outPopUp").fadeOut(3000); } – Razvan Becker Jan 12 '13 at 11:16
  • you should not use localhost then! just try this code : var pathname = window.location.pathname; var search = window.location.search; var a = "/csumb/index.php?page=consultanta"; if((pathname + search) == a) { $("body").fadeIn(3000);alert ('ee'); } else { $("body").fadeOut(3000); } – Mohsen Rabieai Jan 12 '13 at 11:28