在javascript中split() [英] split() in javascript

查看:70
本文介绍了在javascript中split()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

  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?

推荐答案

添加 .toString()可以避免此错误:

TypeError:url.split不是函数

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

在此页面上运行时,输出为:

When run on this very page, the output is:

stackoverflow.com

这篇关于在javascript中split()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆