-1

I have this div in my page that is showing X at top of modal, I like to by clicking this user redirect to homepage. I tried this but the even is not firing at all.

  <div class="modal-content">
    <div class="close" onclick="redirecttologin()">x</div>
......
  </div

this is java script but even the alert is not showing

 function redirecttologin() {
  alert("test");
document.location.href = "/";
 };

I am sure that it has the reference to java script file as other things are working in page.

*problem was class="close".

Alma
  • 3,780
  • 11
  • 42
  • 78

2 Answers2

2

Your code should work properly, the only issue is that you should use window.location instead of document.location. They are both the same, but as documentation says, the document.location is read only property.

Also notice, that your closing <div> tag is missing >. That might be a problem as well.

function redirecttologin() {
  alert("test");
  window.location.href = "/";
};
<div class="modal-content">
   <div class="close" onclick="redirecttologin()">x</div>
</div>
Mateusz Woźniak
  • 1,479
  • 1
  • 9
  • 10
-1

You must use

window.location.href = "/"
Wais Kamal
  • 5,858
  • 2
  • 17
  • 36
  • [`document.location`](http://stackoverflow.com/questions/7857878/window-location-vs-document-location) should work in most browsers. – lonesomeday Jan 18 '17 at 21:26