Quantcast
Channel: Is it possible to use .contains() in a switch statement? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Is it possible to use .contains() in a switch statement?

$
0
0

This is just a simple example of what I'm trying to do:

switch (window.location.href.contains('')) {    case "google":        searchWithGoogle();        break;    case "yahoo":        searchWithYahoo();        break;    default:        console.log("no search engine found");}

If it's not possible/feasible what would be a better alternative?

Solution:

After reading some of the responses I found the following to be a simple solution.

function winLocation(term) {    return window.location.href.contains(term);}switch (true) {    case winLocation("google"):        searchWithGoogle();        break;    case winLocation("yahoo"):        searchWithYahoo();        break;    default:        console.log("no search engine found");}

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images