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,520 points
794 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,840 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,840 points
2,496 views
0 votes
0 answers

How do you reverse a string in-place in JavaScript?

How do you reverse a string in ...READ MORE

May 23, 2022 in Java-Script by Kichu
• 19,040 points
461 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,840 points
18,917 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,840 points
16,737 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,840 points
1,360 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,840 points
4,474 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,840 points
1,033 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,840 points
1,371 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,840 points
665 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP