Multiple string matches with indexOf()
'Video' || 'Audio'
is a logical OR. A non-empty string is implicitly a true value in JavaScript, and thus the short-circuited OR is not evaluated and this collapses to just 'Video'
. This is why you see the results you do.
Others have pointed you in correct directions to resolve.
It's far shorter to turn this into a regular expression.
if ( srcTag.match( /(video|audio)/ ) ) {
/* Found */
} else {
/* Not Found */
}
On a side note, please don't do what you're attempting to do. Asking users to download Safari when they're using Internet Explorer 8 is doing a disservice to the Internet, as well as to that user.
As for redirecting the domain to another location, you should use .preventDefault()
to keep the browser from following the link:
$("a.videoDownload").on("click", function(e){
e.preventDefault();
if ( this.getElementsByTagName("img")[0].src.match( /(video|audo)/ ) ) {
window.location = confirm( 'Download Safari?' )
? "http://apple.com/safari/download"
: "../../../index.html" ;
} else {
/* No match */
}
});
Again, please don't actually do this. Nobody wants to be that guy, and when you tell users to download another browser, you're being that guy.
I think you probably want the OR (||) operator outside indexOf as so:
if ((srcTag.indexOf('Video') !== -1) || (srcTag.indexOf('Audio') !== -1)) {
...
}