Split () in javascript

I have a code:

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

When I run it, I get only one warning message:

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

Why am I not getting a second warning?

+5
source share
7 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 starting on this very page, the output is:

stackoverflow.com
+13
source

The location object is the reason for this, window.location is an object, not a string , it's location.href or location.toString ().

  function _filter() {
    var url = window.location.href; // or window.location.toString()
    alert(url);
    alert(url.split("/")[1]);
  }
+4
source

window.location , href :

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

url ans, , split

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

[1] http://, . [2] - localhost:8000, , , .

window.location.hostname .

+1

, , .lenght url.split, , script ?

firebug, ,

0

url.split( "/" ) [1] null. , (null) msg.

0

All Articles