AlexR AlexR - 3 months ago 15
HTML Question

How to change the style of a div based on the current Domain

I have code that appears on two sites, lets call them www.siteone.com and www.sitetwo.com

I'd like to hide a div but only if the user is on sitetwo.

I have limited access to the html but i can insert code before and after the div i want hidden, including adding other divs and js. jquery is available also.

my thought is something like this:

code inserted before:

<script>
some js that changes the visibility of a div id 'hiddenontwo'
if domain = www.sitetwo.com then set visibility of id=hiddenontwo to hidden
</script>
<div id=hiddenontwo> <!-- open a a wrapper div as i can't set an id on the actual div -->


Code inserted after:

</div> <!-- Closing the wrapper-->


Questions:
is this possible?

as my js knowledge is zero, could someone be kind enough to show how it might be done?

Many thanks in advance

Answer

I think you could write something like that

(function(){
  var divToHide = document.querySelector('#hiddenontwo');
  if(location.host === "www.sitetwo.com") {
     divToHide.style.visibility = 'hidden';
  }
})();
Comments