Marcel Domuta Marcel Domuta - 4 months ago 16
Javascript Question

How to acces an external link without opening it, just loading it

Maybe is a strange title but all I want is to do so:

I have an

arduino webserver
and an
webserver


My
arduino server
can receive data via
url
like

192.168.1.2?light=1
- light on

192.168.1.2?light=0
- light off

It is working just fine, but the problem is when I put that link anywhere on an website (in button or just normal links) the
arduino server
is opening in browser, is there possible to just load it using
ajax
,
js
or
jquery
or just simply using
html
?

Answer

Assuming you have a webpage with jQuery.

HTML

<a class="access-only" href="http://192.168.1.2?light=1">Turn on the light</a>
<a class="access-only" href="http://192.168.1.2?light=0">Turn off the light</a>

JS

$(document).ready(function() {
    // 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's default click action.
        return false;
    });
});
Comments