indexOf()使用出错

chocolateboy 2005-01-16 11:03:31
页面路径:http://gxy/xby/test.htm

var thisUrl = "";
thisUrl = window.location; //如果这一行我写成 thisUrl = "http://gxy/xby/test.htm" 则没问题
if(thisUrl.indexOf("/cn/")!=-1){ //这一行出错,提示对象不支持此属性或方法
...
}

...全文
392 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
boneeater 2005-01-17
  • 打赏
  • 举报
回复
thisUrl = window.location; 改成thisUrl = window.location.url;就OK啦^_^
cxz7531 2005-01-17
  • 打赏
  • 举报
回复
thisUrl = window.location.toString()
或者
thisUrl = ""+window.location;
梅雪香 2005-01-16
  • 打赏
  • 举报
回复
thisUrl = window.location.href;
seeu1688 2005-01-16
  • 打赏
  • 举报
回复
不,window.location确实是对象,只是他的toString()方法会呈现出window.location.href值

alert时对象会默认调用该对象的toString()方法,所以程序字符串

而赋值时也会调用toString()方法的,即对其href属性赋值。

确实不严谨,但dom约定俗成的东西简化倒也并非大问题,只要明白概念
ice_berg16 2005-01-16
  • 打赏
  • 举报
回复
手册上说:
location.href是location的默认属性
设置location='http://microsoft.com' 和设置
location.href='http://microsoft.com'.
是相同的
deerchao 2005-01-16
  • 打赏
  • 举报
回复
thisUrl = window.location.href;
chocolateboy 2005-01-16
  • 打赏
  • 举报
回复
?是对象?为什么alert(window.location);不显示 [object] 而显示路径信息?
或者说我们经常window.location='http://url';。这样写是不严谨的做法?
zhangfire 2005-01-16
  • 打赏
  • 举报
回复
thisUrl = window.location.href;
leojay1 2005-01-16
  • 打赏
  • 举报
回复
enen
seeu1688 2005-01-16
  • 打赏
  • 举报
回复
将location对象转化为它的字符串形式

thisUrl = window.location; => thisUrl = window.location.toString();
meizz 2005-01-16
  • 打赏
  • 举报
回复
window.location 是一个对象
window.location.href 返回的才是当前网页的链接字符串

87,992

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧