How do I tell jQuery to run a function on one specific web page only?

前端 未结 4 1728
臣服心动
臣服心动 2021-02-18 21:57

I have some jQuery code that is on every web page of the site (in the head element).

What selector syntax can I use to tell jQuery that a certain function should only ru

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-18 22:29

    You can't specify the current URL in a jQuery Selector, you can however write a simple check using the document.location object:

    if (document.location.pathname == "/somefolder/somepage") {
       // do some special stuff on this page!
    }
    

提交回复
热议问题