Like "access only" to a link using javascript

I have a server arduino webserverand normal webserver Communication between these servers uses the sistem like link:

Standard web server for Arduiono server:

  • ArduinoServer / light1 = on - light is on
  • ArduinoServer / light1 = off - light off

Arduino web server for a regular web server:

  • NormalWebserver / temperature = 22 & humidity = 56 & light1 = on
  • NormalWebserver / temperature = 22 & humidity = 56 & light1 = off

Comunicaton works well, but the problem is with the action buttons, when I switch the lights, I need the just to accesseternal arduino websever link, and I also use the ajaxlight operator to test the light.

If the light statement is changed, my div.id = "light1" accepts the new javascript content, and my old code to prevent redirecting to arduino webserverno longer works.

The old code 'acces only'contains a link in the button:

// Attach click handler to all 'access-only' links.
$('a.access-only').click(function() {
    // Once the link is clicked, access its URL with a GET request.
    $.get($(this).attr('href'), function(response) {
        // Do nothing here, the URL has been accessed.
    });

    // Return false to prevent the browser default click action.
    return false;
});

But I need code for this, even with new content from ajax.

+4
source share
1 answer

istead , . $.get . , . preventDefault click a.

$(document).on('click', 'a.access-only', function(e) {
    e.preventDefault();
    $.get($(this).attr('href'));
    return false;
});
+4

Source: https://habr.com/ru/post/1652838/


All Articles