How to check if the URL contains a given string

655    Asked by KaylaShinn in Python , Asked on Jul 27, 2021

How could I do something like this:

[removed]
$(document).ready(function () {
    if[removed].contains("franky")) // This doesn't work, any suggestions?
    {
         alert("your url contains the name franky");
    }
});
[removed]

Answered by Pippa Kelly

 To check if the URL contains a given string you can use indexOf like this:

    if[removed].href.indexOf("franky") != -1){....}

Also notice the addition of href for the string otherwise you would do:

    if[removed].toString().indexOf("franky") != -1){....}


Your Answer

Interviews

Parent Categories