最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Correct usage of IndexOf in JavaScript - Stack Overflow

programmeradmin1浏览0评论

I've got a simple JavaScript statement that reads a string and fires off a URL based of what it contains but I'm not sure if I've used IndexOf correctly so I just wanted to check.

Here is my code snippet:

<script type="text/javascript"> 

var mc_u1 = "somevariable";

if (mc_u1.indexOf("1|Accept") > 0) {
document.writeln("<img src=\"\">");
}

if (mc_u1.indexOf("1|Refer") > 0) {
document.writeln("<img src=\"\">");

}

if (mc_u1.indexOf("2|Accept") > 0) {
document.writeln("<img src=\"\">");

}

if (mc_u1.indexOf("2|Refer") > 0) {
document.writeln("<img src=\"www.someurl4\">");

}

</script>

As you can see from the code above, what I'm trying to do is based on the contents of the variable mc_u1 fire off a URL (which are different though I've just masked them for obvious reasons).

My question is, if the mc_u1 variable begins with say 1|Accept should I be using > -1 in the Javascript Statement or > 0?

Hope this is making sense!

I've got a simple JavaScript statement that reads a string and fires off a URL based of what it contains but I'm not sure if I've used IndexOf correctly so I just wanted to check.

Here is my code snippet:

<script type="text/javascript"> 

var mc_u1 = "somevariable";

if (mc_u1.indexOf("1|Accept") > 0) {
document.writeln("<img src=\"https://www.someurl1.\">");
}

if (mc_u1.indexOf("1|Refer") > 0) {
document.writeln("<img src=\"https://www.someurl2.\">");

}

if (mc_u1.indexOf("2|Accept") > 0) {
document.writeln("<img src=\"https://www.someurl3.\">");

}

if (mc_u1.indexOf("2|Refer") > 0) {
document.writeln("<img src=\"www.someurl4.\">");

}

</script>

As you can see from the code above, what I'm trying to do is based on the contents of the variable mc_u1 fire off a URL (which are different though I've just masked them for obvious reasons).

My question is, if the mc_u1 variable begins with say 1|Accept should I be using > -1 in the Javascript Statement or > 0?

Hope this is making sense!

Share Improve this question asked May 21, 2012 at 9:24 zikzik 3,09511 gold badges44 silver badges62 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

From MDN:

string.indexOf(searchValue[, fromIndex])

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.

So in order to check if your variable starts with "1|Accept" you should check if indexOf returns 0.

0 is the index of the first letter, so if your substring appears at the beginning of the string, > 0 will not match. So use > -1 for anywhere in the string and == 0 for always at the start of the string.

You should use == 0 since at the beginning, the index will be 0

mc_u1.indexOf("1|Accept") > 0 will also be true if the string only contains substring somewhere. It will be -1 if the substring wasn't found.

https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Array/indexOf

发布评论

评论列表(0)

  1. 暂无评论