Switch statement for string matching in JavaScript

0 votes

How do I write a switch for the following conditional?

If the url contains "foo", then settings.base_url is "bar".

The following is achieving the effect required but this would be more manageable in a switch:

var doc_location = document.location.href;
var url_strip = new RegExp("http:\/\/.*\/");
var base_url = url_strip.exec(doc_location)
var base_url_string = base_url[0];

//BASE URL CASES

// LOCAL
if (base_url_string.indexOf('dey.local') > -1) {
    settings = {
        "base_url" : "http://dey.local/"
    };
}

// DEV
if (base_url_string.indexOf('dey.dev.yyy.com') > -1) {
    settings = {
        "base_url" : "http://dey.dev.yyy.com/xxx/"
    };
}
Jun 2, 2020 in Java-Script by kartik
• 37,490 points
102 views

1 answer to this question.

0 votes

Hello @kartik,

Just use the location.host property

switch (location.host) {
    case "dey.local":
        settings = ...
        break;
    case "dey.dev.yyy.com":
        settings = ...
        break;
}

Hope it work!

answered Jun 2, 2020 by Niroj
• 82,560 points

Related Questions In Java-Script

0 votes
1 answer

How do I search for a key of object in javascript?

Use hasOwnProperty(key) for (let i = 0; i ...READ MORE

answered Oct 14, 2020 in Java-Script by Niroj
• 82,560 points
238 views
0 votes
1 answer

What is callback function in JavaScript?

callback is not a keyword, its just ...READ MORE

answered Jan 30, 2020 in Java-Script by Niroj
• 82,560 points
3,751 views
0 votes
1 answer

How do I turn a string to a json in Node.js?

Hello Kartik, Use the JSON function  JSON.parse(theString) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,560 points
144 views
0 votes
1 answer

How can we access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?>

Hello @kartik, You can also use json_encode for ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,560 points
3,706 views
0 votes
1 answer

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

Hii, You can use Conditions using Array: $users = User::where([ ...READ MORE

answered Mar 30, 2020 in Laravel by Niroj
• 82,560 points
6,007 views
0 votes
1 answer

How to send email using php?

Hello @kartik 1.) Download PHPMailer, open the zip file ...READ MORE

answered Apr 1, 2020 in PHP by Niroj
• 82,560 points
198 views
0 votes
1 answer

Where to register Facades & Service Providers in Lumen?

Hello, To register a facade with an alias, ...READ MORE

answered Apr 6, 2020 in Laravel by Niroj
• 82,560 points
1,045 views
0 votes
1 answer

How can I update NodeJS and NPM to the next versions?

Hello @kartik, First check your NPM version npm -v 1).Update ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,560 points
127 views
0 votes
1 answer

How can I get query string values in JavaScript?

Hello @kartik, You don't need jQuery for that ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,560 points
107 views
0 votes
1 answer

How do I break a string across more than one line of code in JavaScript?

Hello @kartik, In your example, you can break ...READ MORE

answered Oct 8, 2020 in Java-Script by Niroj
• 82,560 points
86 views