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 in Java-Script by kartik
• 15,480 points
27 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 by Niroj
• 32,240 points

Related Questions In Java-Script

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 in Java-Script by Niroj
• 32,240 points
275 views
0 votes
1 answer

How do you access the matched groups in a JavaScript regular expression?

Hello, Here’s a method you can use to ...READ MORE

answered May 28 in Java-Script by Niroj
• 32,240 points
28 views
0 votes
1 answer

How to get the browser to navigate to URL in JavaScript?

Hii, This works in all browsers: window.location.href = '...'; If ...READ MORE

answered May 28 in Java-Script by Niroj
• 32,240 points
43 views
0 votes
1 answer

How do you cache an image in Javascript?

Hii @kartik, You have to do three thigs: You ...READ MORE

answered Jun 2 in Java-Script by Niroj
• 32,240 points
23 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 in Laravel by Niroj
• 32,240 points
491 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 in PHP by Niroj
• 32,240 points
39 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 in Laravel by Niroj
• 32,240 points
152 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 in Java-Script by Niroj
• 32,240 points
41 views
0 votes
1 answer

What is callback function in JavaScript?

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

answered Jan 30 in Java-Script by Niroj
• 32,240 points
383 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 in Java-Script by Niroj
• 32,240 points
33 views