I am returning cookies through a Chrome Extension - one of the cookies.name is use_hitbox - so naturally I want to do:
if (cookie.name.indexOf("use_hitbox") > 0) {
alert("FOUND HITBOX COOKIE");
}
The Issue is:
cookie.name.indexOf("use") returns true
cookie.name.indexOf("hitbox") returns true
cookie.name.indexOf("use_hitbox") returns false
Any ideas?
Disclaimer:
This is for use on MY site, nothing malicious...!
EDIT: Cool, this works - but the underlying issue was Chrome Caching my Extensions file
indexOf() will return 0 if the match is at the beginning of the string. -1 indicates no match so try:
if (cookie.name.indexOf("use_hitbox") > -1)
Rather than parsing the returned index as boolean (which as pointed out, will mean an index of 0 parses as False), compare result != -1, which is the return value if the substring is not found.
Related
I want to trigger a digital marketing tag on every page which falls under a particular URL path, say example.com/sachin under the sachin directory.
I've tried if (location.href === 'example.com/sachin.*/') but somehow the condition doesn't seem to work.
What will be the correct if condition for location.href if I want to cover all different resources with in the URL path say under sachin directory?
I presume you want to check if the URL contains example.com/sachin. It's highly rarely that any URL ever would contain 4 forward-slashes but what you would do is utilize indexOf.
if(location.href.indexOf("example.com/sachin/") != -1){
//Do something
}
This basically says, if "example.com/sachin/" is found somewhere in the given string(in location.href in this case) on an indexposition that is not -1(which means that it doesn't exist), then execute.
You need to use Regular Expressions to match needed resources.
Something like that:
if(location.href.match(/^http:\/\/example.com\/sachin\//)){
//your staff here
}
Another approach to check for a specific directory in a url.
function urlContains(url, value) {
return ~url.indexOf(value);
}
if (urlContains(location.href, "/sachin/")) {
// found
} else {
// not found
}
The indexOf method checks a string for the value that is passed and returns -1 if a result was not found.
Instead of checking for == -1 or != -1 you can use the Bitwise NOT operator ~ to convert -1 to 0, which is a falsy value, non-zero values are treated are truthy values in javascript.
I came across this line of code recently, and want to understand what it means and does, as my javascript-foo is not that hot :
if ((+!!config.template) + (+!!config.templateUrl) !== 1) {}
from what I can gather, it is checking to see if either option is set (so either template, or templateUrl must be set, not both or none)
so, if config.template was set,
+config.template would not work (template is not a number)
!config.template would return false (-1)
!!config.template would return true (0)
+!!config.template would therefore return 1
if config.template was not set,
!config.template would return true (0)
!!config.template would return false (-1)
+!!config.template would therefore return 0
if then you were to apply the same to config.templateUrl , you would end up with 1 if set, 0 if not
So the final test is to see if the sum of config.template and config.templateUrl is 1 (ie one or the other is set)
Is this valid reasoning ?
The bool value is being cast to a Number by prepending it with +.
!! in the code above is checking for existence of the property template on config. If there is no template found !! would usually return false, however by prepending +, it returns 0. Both +!! statements return numbers, which when they're added together will either be 0 or 1.
The final check will return true if both or none were set / true (!== 1)
While I am writing some JavaScript, I came across a new code "indexOf". After read another post I thought its behaviour is as shown below but it seems not true. Can someone kindly give me an explanation about "indexOf", please?
false = -1;
true = 0 and more?
I have tried to change -1 to 0 and more but then nothing happens. Just to have a better understanding about jquery/indexOf.
what I have now,
$(this).closest(row)[td_word.indexOf(keyword) !== -1 ? 'show' : 'hide']();
it search for match(es) of "keyword" from "td_word",
if it is not false (!== -1, thus true) display:visible;,
if it is not true (false) display:hide;.
Thanks in advance.
array.indexOf(element) returns the index of the element in the array. Read the official documentation as well.
It was designed to return -1 when the element doesn't exist, because 0 would mean that the element is in the 0th index (1st element).
Examples :
var array = ['a','b','c','d','e'];
array.indexOf('a') //0
array.indexOf('c') //2
array.indexOf('f') //-1, because it doesn't exist in array
From what I understand in your wording, I think that you think that indexOf is used to check if a certain element exists in an array. That is just a "side-effect" of indexOf but its actual usage is getting the index of an element in the array.
I was wondering, say I had some thing like the following:
console.log(element.find('div').eq(3).text().indexOf('whatever'));
bearing in mind that element is defined and the console is logging a value of 32 (or anything that isn't -1) what would be the best way of converting the result into a Boolean so my console.log either outputs true or false
Thanks in advance.
The answer above will work, however if you are as nerdy as I, you will far prefer this:
console.log(~element.find('div').eq(3).text().indexOf('whatever'));
The obscure '~' operator in javascript performs the operation "value * -1 - 1", e.g. ~-2 === 1. The only use case I have ever had for this is in converting the "not found" -1 from ".indexOf()" into "0" (a falsey value in javascript), follow through and see it will convert an index found at position "0" to "-1", a truthy value.
tldr:
~[1,2,3].indexOf(0) // => 0
!!~[1,2,3].indexOf(0) // => false
~[1,2,3].indexOf(1) // => -1
!!~[1,2,3].indexOf(1) // => true
console.log(element.find('div').eq(3).text().indexOf('whatever') > -1);
I'm trying to find out if a string exists like so:
var test1 = '{"packageId":"1","machineId":"1","operationType":"Download"},{"packageId":"2","machineId":"2","operationType":"Download"}';
alert("found: " + test1.indexOf('{"packageId":"1","machineId":"1","operationType":"Download"}', 0));
However, the result is always 0.
what gives?
Just in case this isn't a joke...
String.prototype.indexOf returns the occurence of a matching string within a target string, since you just look for the very first occurence of that line, it correctly returns zero.
If you modify your search-string (for example with some random letters), you will get -1 as result since it will not get found.
There is a practice using the binary not operator, to pretty much bring the result from .indexOf() down to a boolean expression. This would look like
var res = test1.indexOf('{"packageId":"1","machineId":"1","operationType":"Download"}');
if( ~res ) {
// we have a match
} else {
// no match at all
}
Without going into great detail, the not-operator will negate each bit from a byte, also the extra bit which is used to determine if the value is positive or negative. So, since in ECMAscript only very few values are evaluated to falsy values, negative values will evaluate to true.
To really have a boolean result, it would look like
if( !!~res ) { }
which again is not really neccessary in this case.
The far more often used practice to get the "right" result using .indexOf() (same goes for Arrays), is to check if the result is greater than -1
if( res > -1 ) { }
ya its correct indexOf will return starting index of the string you mention, thats y its gives 0. If string is not exist it return -1
Some sample example
var sample= "welcome to javascript";
alert ( sample.indexOf("welcome",0)); // return 0
alert ( sample.indexOf("come",0)); // return 3
alert ( sample.indexOf("came",0)); // return -1
alert ( sample.indexOf("javascript",0)); // return 11
Match :
if(sample.indexOf("welcome",0)>-1)
alert("match");
else
alert("Not match")l