Why does the onclick function not work on a hyperlink?

Asked

Viewed 110 times

1

I’m trying to create an icon that when there’s one click the information is saved in it, through a sessionStorage, and at the same time be redirected to another page where I will fill it through the information of sessionStorage. I’m wearing this div:

<a href="icone.html" onclick=pegarInfo()>

Could someone explain to me why it doesn’t work? and how I can get around this situation?

  • Caro Igor, pegInfo won’t work because href probably directs before javascript runs (cc @Marceloboni)

  • @sam is a function that only stores the information with a sessionStorage.

  • Ever tried to put folder directory? How ..\icone.html

  • @Sam, considering the remaining 1%, would put a window.location in function pegarInfo()

  • Beauty, if he puts an Alert in the function will see that the onclick works before the href

  • 1

    @sam works intermittently or that oscillates is not working, the moment that redirect page, even if some script is running something will be destroyed may or may not fail, that there is no sense and nor should be done, things have to be planned to work and not by "maybe it works" or "think it works". Simply href="icone.html" will redirect and at this point any attempt to execute script on a page that is downloading to load another will be a factor of "competition" no guarantees.

  • @sam doing right is quite different from what this done there, so I’ve already commented will not work, because simply have to redo. It’s a matter of seconds, there’s no right way about it, and there’s no reliance on a browser.

  • @Igor would probably be better to explain what you want to do. Normally either you use href, or the onclick. There will hardly be any real reason to use both (maybe you have the impression of being necessary in your case, but it’s probably lack of knowing a real solution - probably to put the redirect on the JS).

  • @Leocaracciolo com window.location.assign("imovel.html") works perfectly, thank you very much.

  • @Bacco well I’m wanting that when the person clicks on the link the browser save the information and redirect the person to another page, and on the page that was redirected I can fetch the information from the browser and complete the gaps like "Name" etc.

  • @sam is what Bacco said and what I said, is the timing, the same as what I said: "It’s a matter of seconds" or less than seconds. This is called "competition" in the running time of two tasks.

  • Igor Costa is the most sensible way to apply (window.Location within the function) . See how your question yielded a long discussion rs

  • @Igorcosta in this case really seems to me that Leo’s solution is appropriate, take the href and leave the link in Location. So it’s guaranteed to run the code you want, and Location will play its role only later. And yet you have to see if the code executed is synchronous. If it is asynchronous, it can still give a timing problem (Location kills your JS before it is finished).

  • I happen to remember a post on facebook of a certain moderator of the network that I shared.... your comment "raising the possibility of functioning normally", Wrong is still wrong even if everyone does it wrong, and right is still right even if nobody does it right.

Show 9 more comments

1 answer

3

Considering @Guilherme Nascimento’s comments:

works intermittently or that oscillates is not working, at the moment that redirect page, even if some script is running something will be destroyed may or may not fail, that there is no sense and nor should be done, things have to be planned to work and not by "maybe it works" or "think it works". Simply href="icone.html" will redirect and at this point any attempt to execute script on a page that is downloading to load another will be a factor of "competition" no guarantees.

I suggest you remove href and put window.Location in the pick functionInfo()

Why use or a onclick or another href?

Imagine a scenario of competition

function pegarInfo(){
  setTimeout(function(){
      alert("isso deve executar dentro de 2 segundos");
  }, 2000);
}
<a href="icone.html" onclick="pegarInfo()">Teste</a>

function pegarInfo(){
  setTimeout(function(){
      alert("isso deve executar dentro de 2 segundos");
      window.location = "icone.html";
  }, 2000);
}
<a onclick="pegarInfo()">Teste</a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.