Why isn’t the code working?

Asked

Viewed 76 times

2

I want, by means of a script (Javascript pure and simple), alert after the click in a given link (class "get out of") The following: "You are leaving the site".

Man script is like this:

window.document.querySelectorAll('.**sair**').onclick = function aviso() {
     alert('Você está saindo do site.');
}; 

The class "get out of" is in the HTML markup section, below:

<p><a href="http://www.google.com" title="www.google.com" class="botao sair">Google</a></p>

When I activate the link, he alerts absolutely nothing.

Observing

I tried with the script:

window.document.getElementsByClassName('sair').onclick = function aviso() {
    alert('Você está saindo da página.'); 
 };

And it didn’t work either.

What is wrong?!

From now on, thank you,

Alexandre Soares

  • You’re calling the class botao sair in HTML (class="botao sair"), and using .sair (.'sair').onclick) in javascript. At least one of the problems should be this (I tested here changing and still not opened). But soon someone will come up with the solution. :-)

  • Thank you, Gustavo!

1 answer

1


If you have more than one link, I see that you are using querySelectorAll, then you have to use a cycle for to add the event receiver to each element. This is because the querySelectorAll gives a list of elements (type Array).

Something like that:

var links = document.querySelectorAll('.sair');
for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('click', aviso);
}

function aviso(e) {
    alert('Você está saindo do site.');
};

jsFiddle: http://jsfiddle.net/f8mr69jt/

You can also use your syntax links[i].onclick = aviso; (jsFiddle)

If you have only one element you have to use querySelector (without the All) and then your code already works, that means you don’t need the cycle for.

  • It’s true, there’s more than one link in the website, yes. I tried your script And nothing happened either, pal. Replace "var links = Document.querySelectorAll('.exit');" with "var links = Document.getElementsByClassName('exit');", unfortunately, without success. Thank you, anyway, for your suggestion, Sergio. Funny that in theory everything is all right. I inspected with the "Developer Tools" of Chrome and it presented no error in the construction of scripts. Will know...!...

  • @Alexandresoares you’re not really using asterisks here 'querySelectorAll(.**sair*');*"pos not?

  • Do you have that script where on the page? on <head> ? <body>?

  • I didn’t use the asterisks, no. I was just trying to format the comment. The script is local, in HEAD, really. Because I’m testing.

  • @Alexandresoares there is the problem! Put at the bottom of the page, before the </body>

  • Now it worked! But, what if I want to put the script in an external file, will crash?

  • @Alexandresoares in this case does so: window.onload = function() { /* o teu código aqui */ }, and that way the script will run when the page has loaded. Otherwise querySelectorAll does not yet find HTML. Test like this in head also that will work.

  • @Alexandresoares I leave now, if the answer solved p problem you can mark the answer as accepted if you want.

  • 1

    Now it’s OK! Very grateful, Sergio! You’re a skull! It’s perfect! Strong hug, Alexandre

Show 4 more comments

Browser other questions tagged

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