简体   繁体   中英

How to check if the URL in android browser contains a given string?

I am trying to check if a string is part of the page URL in various android browsers using the following code. It doesn't work in any android device. When I check in iOS devices or in desktop (windows OS) browsers it works fine.

Any ideas??

<script type="text/javascript">
$(document).ready(function () {
    if(window.location.href.indexOf("apieceofstring") > -1){
         alert("success");
    }
});
</script>

The indexOf() method returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex. Returns -1 if the value is not found .

it mean your statement always true you need to do something like this

if (window.location.href.indexOf("apieceofstring") > -1) {
  alert("success");
}

First of all if you want to program in android then write your code in java. Secondly use regex for finding out if a string contains another sub-string http://www.tutorialspoint.com/java/java_regular_expressions.htm

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