この例が見つかりました:https://www.kirupa.com/html5/examples/detecting_internet_connection.htm インターネットが存在するかどうかを警告します。接続が存在するが、リンクを開くこと。初心者のJavascriptヘルプ、条件が真である場合にリンクを開くための文
<div id="mainContainer">
<a href="javascript:void(0)">Check connection!</a>
</div>
var link = document.querySelector("#mainContainer a");
link.addEventListener("mousedown", checkConnection, false);
function checkConnection(e) {
if (doesConnectionExist() == true) {
alert("connection exists!");
} else {
alert("connection doesn't exist!");
}
}
function doesConnectionExist() {
var xhr = new XMLHttpRequest();
var file = "http://www.kirupa.com/images/giant_cloud.png";
var randomNum = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + randomNum, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
は、私は何をしたいのである:
<a href="javascript:void(0)">Check connection!</a>
は、www.google.comのようなリンクを入れて、中:何かにアラートを
if (doesConnectionExist() == true) {
alert("connection exists!");
変更することをwww.google.com
これはWebアプリケーションで使用されます。あなたは
window.open( "www.google.com")を使用することができます
ありがとう、しかし、それは、Javaではなく、私はhrefのURLを書くことが可能ですか複数のリンクのために私は各リンクのステートメントを作成する必要がありますか? – Benedetto
あなたはそれをさらに説明できますか? –