Home javascript How to redirect in js?

How to redirect in js?

Author

Date

Category

Good day! There was such a problem. Redirect does not work in js

& lt; a id = "aaa" onclick = "href ();" href = "#" & gt; & lt; / a & gt;

Tried these ways but nothing helps

& lt; script language = "JavaScript" & gt;
  function href () {
  window.location.replace ("http://stackoverflow.com");
  document.location.href = 'http: //stackoverflow.com'
  }
& lt; / script & gt;

Answer 1, authority 100%

The problem is that inline handlers are executed in the context of an HTML element.

that is, whatever is used is first searched for in the html element, and in this case href is a property of the a element, not a function.

You can easily check it, just change the code to the following

& lt; a id = "aaa" onclick = "console.log (this.href = == href, href); " href = "#" & gt; Click me & lt; / a & gt; 

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions