aldegalan
aldegalan

Reputation: 502

Change html information displayed depending of the environment

Having two environments:

http://www.example1.com
http://www.example2.com

And having the same html for both environments:

<b>We are in $variable</b>

Is it possible to display the information depending of the environment were we are?

For example: If we are in example1, show:

<b>We are in the first environment</b>

If we are in the second, then:

<b>We are in the second environment</b>

Upvotes: 0

Views: 89

Answers (3)

Robson
Robson

Reputation: 2032

A potential solution would be to look at the host and then act accordingly. Like so:

if (document.location.host == "stacksnippets.net") {
  document.getElementById('number').innerHTML = "first";
} else {
  document.getElementById('number').innerHTML = "second";
}
<p>We are in the <span id="number">unknown</span> environment.</p>

Tested successfully on desktop in Firefox, Chrome, Opera and Edge. Tested successfully on mobile in Chrome.

Upvotes: 2

androidoverlord
androidoverlord

Reputation: 301

here is a check with javascript

const url = window.location.href;
const element = document.getElementById('element');

if( url.includes('example1') ){
element.innerHTML = 'we are in example 1';
}else{
element.innerHTML = 'we are in example 2';
}
<div id="element"></div>

Upvotes: 1

MetropolisCZ
MetropolisCZ

Reputation: 605

Another solution could be using PHP:

if ($_SERVER['SERVER_NAME'] == "www.example1.com") {
    $variable = "the first environment";
}
else {
    $variable = "the second environment";
}

Place it in the beginning of page, and then you can use value of $variable like this:

<b>We are in <?php echo $variable ?></b>

Upvotes: 1

Related Questions