Reputation: 39
I like to trigger/open a Link action (the link activates a LED) but I don't want to open the links website. It there an option to do it? Like opens the link in the background?
<a id='button' href="http://192.168.178.123/5/on:80">Button</a>
Is it possible with JS or other languages?
Thanks!
Upvotes: 1
Views: 6740
Reputation: 1
Sorry for writing to this old thread, but it has no accepted answer so...
My answer works by creating dummy iframe and directing the link in it. If you want to show some response from the device (the webpage or http response device shows when you enter it normally) just remove style="display:none"
from the iframe.
<iframe style="display:none" name = "httpRequestDummy" src = ""></iframe>
<a href = "http://192.168.178.123/5/on:80" target = "httpRequestDummy">Switch ON</a>
Upvotes: 0
Reputation: 1
I needed something similar for a different purpose and I did it this way:
in Head:
<script>
function sleep(ms) { //wait function
return new Promise(resolve => setTimeout(resolve, ms));
}
async function ninjaLink() { //doing sneaky stuff
var ifrm = document.createElement("iframe"); //Creating iFrame
ifrm.setAttribute("src", "YOUR_URL"); // Setting iFrame's source
ifrm.style.display = "none"; //Hiding iFrame
document.body.appendChild(ifrm); //Adding iFrame
await sleep(3000); //Giving the web page time to load
document.body.removeChild(ifrm); //Removing iFrame
}
</script>
in Body:
<a href="javascript:ninjaLink()">Link</a>
It opens the link in an hidden iFrame
then destroys it.
Upvotes: 0
Reputation: 51
You can listen the event that triggers when button's clicked and send request using javascript.
document.getElementById("button").addEventListener("click", (e) => {
e.preventDefault();
const request = new XMLHttpRequest();
const url = 'http://192.168.178.123/5/on:80';
request.open("GET", url);
request.send();
});
<a id='button' href="#">Button</a>
Upvotes: 0
Reputation: 395
You can use javascript ajax call.
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "http://192.168.178.123/5/on:80", false);
xhReq.send(null);
var serverResponse = xhReq.responseText;
Here is example how to bind function to button
<button onclick="myFunction()">Click me</button>
<script>
function myFunction() {
//Ajax call here
}
</script>
You can read more here: https://www.w3schools.com/jsref/event_onclick.asp
Upvotes: 1
Reputation: 53
try this
<a id='button' href="http://192.168.178.123/5/on:80" onClick="fn(event)">button</a>
function fn(e){
e.preventDefault();
}
Upvotes: 0
Reputation: 11
Just setting the href to "#" will stop the browser going anywhere.
Upvotes: 1