基本上,我想检查用户使用的是什么url并将其与默认值进行比较,然后运行一条语句

假设我的网址是https://stackoverflow.com/questions/ask,我正在尝试执行此操作,但它不起作用:

<script type="text/javascript">
      if(document.location.href('/questions/ask') > 0)
  {
    [do this];
  }


感谢您的帮助(我知道的菜鸟问题)

最佳答案

试试看,您错过了indexOf方法。

if(document.location.href.indexOf('/questions/ask') > -1)


但是我相信您应该离开该窗口对象,我认为文档已过时(但仍然有效)。

if(window.location.href.indexOf('/questions/ask') > -1)


您还希望检查索引是否大于负1,因为从技术上讲零是正确的位置。

10-02 09:10
查看更多