Last active
November 23, 2017 00:17
-
-
Save SoldierCorp/5887679 to your computer and use it in GitHub Desktop.
Comprobar si hay conexión a Internet utilizando jQuery.Ajax con callback. --- Check internet connection using jQuery.Ajax with callback.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
var conn = true; | |
checkConnection(); | |
function checkConnection() { | |
$.ajax({ | |
url: 'http://yoursite.com/process.php', | |
async: false, | |
data: {'tag' : 'connection'} | |
}) | |
.fail(function() { conn = false; }) | |
.done(function() { conn = true; }) | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Access-Control-Allow-Origin: *'); | |
$callback = isset($_GET['callback']) ? preg_replace('/[^a-z0-9$_]/si', '', $_GET['callback']) : false; | |
header('Content-Type: ' . ($callback ? 'application/javascript' : 'application/json') . ';charset=UTF-8'); | |
if (isset($_REQUEST['tag']) && $_REQUEST['tag'] != "") { | |
$tag = $_REQUEST['tag']; | |
$json = array(); | |
if ($tag == 'connection') { | |
$json['success'] = 'success'; | |
$json = json_encode($json); | |
echo $callback ? "$callback($json)" : $json; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment