【问题标题】:JQuery IF document url = "../products.html" Then do thisJQuery IF 文档 url = "../products.html" 然后这样做
【发布时间】:2011-05-19 15:13:42
【问题描述】:

如何获取文档 url 并将其与条件分支进行比较?

伪代码:

If document url = "../products.html"
Then do this piece of code();

【问题讨论】:

    标签: javascript jquery if-statement conditional


    【解决方案1】:

    如果你正在使用加载的html,你可以试试

    if(window.location.pathname == "products.html"){
    //do something here
    }
    

    location.pathname 返回位置指定的文件名或路径。

    另外,可以通过

    获取文件名
    var p = window.location.pathname.split("/");
    var filename = p[p.length-1];
    

    【讨论】:

    • 感谢您的回答 WaiLam 但这不起作用.. 这个分叉的结果是,页面像每次一样重新加载.. 我现在无法帮助我。我认为 IF 只是用于条件..现在它实际上做了一些事情..有没有另一种方法来获取浏览器的 url,然后将其与 if 语句进行比较?
    • 它返回的是路径而不是文件名,因此您必须与/products.html(或任何绝对路径)进行比较
    • @Dr.Molle:是的。 @先生。 Freeman:另外,可以通过 var p = window.location.pathname.split("/"); 获取文件名文件名 = p[p.length-1];
    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 2014-07-31
    • 2015-01-20
    • 1970-01-01
    • 2011-08-11
    • 2017-09-22
    • 2010-12-17
    • 1970-01-01
    相关资源
    最近更新 更多