Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split() in javascript

Tags:

javascript

I have code:

  function _filter() {
    var url = window.location;
    alert(url);
    alert(url.split("/")[1]);
  }

When I launch it I get only one alert message:

http://localhost:8000/index/3/1.

Why I don't get the second alert message?

like image 232
ceth Avatar asked Nov 23 '10 07:11

ceth


2 Answers

Adding .toString() works and avoids this error:

TypeError: url.split is not a function

function _filter() {
    var url = window.location;
    alert(url);
    alert(url.toString().split("/")[2]);
}

When run on this very page, the output is:

stackoverflow.com
like image 194
Sarfraz Avatar answered Oct 13 '22 23:10

Sarfraz


The location object is the cause of this, window.location is an object not a string it is the location.href or location.toString().

  function _filter() {
    var url = window.location.href; // or window.location.toString()
    alert(url);
    alert(url.split("/")[1]);
  }
like image 25
jerjer Avatar answered Oct 13 '22 23:10

jerjer