Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple string matches with indexOf()

Tags:

Im pretty sure my syntax this wrong because the script only works if the string matches "Video", if the string has the "word "Audio" it is ignored. Also since the href tags have a value of "#" the redirect for "../../../index.html" doesnt work.

js

var ua = navigator.userAgent.toLowerCase(); var isIE8 = /MSIE 8.0/i.test(ua); if (isIE8) {     $('a').click(function () {         var srcTag = $(this).find('img').attr('src');         if (srcTag.indexOf('Video' || 'Audio') > -1) {             if (confirm('Download Safari? \n\n http://apple.com/safari/download/')) {             window.location = 'http://apple.com/safari/download/';             } else { window.location = '../../../index.html';}         } else {             alert('no match');         }     }); } 

html

<a href="#"><img src="Video/000_Movies/assets/005_CCC_Jesus_Story_80x60.jpg" />test1</a>  <a href="#"><img src="Audio/000_Movies/assets/006_GSP_Gods_Story_80x60.jpg" />test2</a>  <a href="#"><img src="Media/000_Movies/assets/002_God_Man_80x60.jpg" />test3</a> 
like image 551
Blainer Avatar asked May 10 '12 15:05

Blainer


People also ask

How many indexOf () methods does the string class have?

Java String indexOf() There are four variants of indexOf() method.

What does indexOf () Do Java?

The indexOf() method returns the position of the first occurrence of specified character(s) in a string.

What are the differences between indexOf () and lastIndexOf ()?

The indexOf() and lastIndexOf() function return a numeric index that indicates the starting position of a given substring in the specified metadata string: indexOf() returns the index for the first occurrence of the substring. lastIndexOf() returns the index for the last occurrence of the substring.

What indexOf () will do?

indexOf() The indexOf() method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring.


2 Answers

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.

like image 87
Sampson Avatar answered Oct 06 '22 23:10

Sampson


'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.

like image 38
David M Avatar answered Oct 06 '22 21:10

David M