简体   繁体   中英

How can we use find() API to find a string in the page URL.?

I have a page URL and want to use find() API and find string present in the URL.

Code:

var pageURL = document.URL;

$(pageURL).find('ArticleID');

With above code, am getting an error:

need to find if the string ArticleID is present in the URL or not.

Is there a better way to do it than using find() ?

Find is a jQuery method used to find elements in the DOM, not in strings. You don't need jQuery for this, use indexOf:

if(document.URL.indexOf("ArticleID") >= 0) {
    // ArticleID exists in document.URL
}

indexOf returns the first position of the input string, or -1 if it's not found.

尝试

var isContains = pageURL.indexOf('ArticleID') > -1;
 if (document.URL.indexOf('ArticleID') === -1) {
  // insert code
  }
$(selector).find(selector)

The JQuery find Method takes a selector and is not what you are looking for.

Take a look at the Javascript indexOf or search

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM