-2

I need some help. I have written a javascript that check out the URL and if the url equals example.com then the script should change the height and the margin of a div element. It works but it keeps reloading the site all the time and I can't click on anything.

Also how can I get a class? getElementsByClassName doesnt work.

function verschieben() {
    if (window.location.href = "example.com") {
        document.getElementById('bigimg').style.height = "200px";
        document.getElementById('searchbox').style.marginTop = "30px";
    }
}

verschieben();
Nicolae Olariu
  • 2,487
  • 2
  • 18
  • 30
TestBlub
  • 7
  • 1

2 Answers2

0

If you want to get host information of current page then window.location.host will have that value, href will contain full url

if (window.location.host.toString() == "example.com")
Maddy
  • 791
  • 1
  • 8
  • 22
0

Your code for class manipulation should look like:

function reshapeDivs(){
    var divs =  document.getElementsByClassName("someClass");
    for(i=0;i<divs.length;i++){
     divs[i].style.height = "200px";
     divs[i].style.marginTop = "30px"
    }
}
<div class="someClass" style="border:1px solid red">blah1</div>
<div class="someClass"  style="border:1px solid black">blah2</div>
<button id="reshapeButton" onclick="reshapeDivs()">reshape divs</div>
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84