Last active
September 21, 2017 17:29
-
-
Save sosroInSpace/d1618bb9159be869db155a8f05557f55 to your computer and use it in GitHub Desktop.
onclick = jQuery API AJAX call with error inclusions - dictionary definitions
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
$('.defineThis').on('click',function (){ | |
$('#texter').css('display','block'); | |
var z = $(this).attr('data-id'); | |
$.ajax({ | |
type:'GET', | |
dataType: 'XML', | |
url :'http://www.dictionaryapi.com/api/v1/references/collegiate/xml/'+ z +'?key=XXXXXXXXXXXXXXXXXXXXXXXXXXX', | |
success: function(xml) { | |
var d = $(xml).find("dt"); | |
var g = 'https://www.google.co.th/search?q='; | |
var t = '+definition&oq=something+definition&aqs=chrome..69i57j0l5.2773j0j7&sourceid=chrome&ie=UTF-8'; | |
var resultU = g + z + t; | |
var m = "sorry we don't seem to have the definition for '"; | |
var v = "' please click the link below for an alternate source"; | |
var h = "Not satisfied? check '"; | |
var j = "' alternate definition"; | |
var resultA = h + z + j; | |
var errorM = m + z + v; | |
var i = xml.getElementsByTagName("dt")[0]; | |
// if no tag found do this --> | |
if( !d.length){ | |
$('#texter').css('display','none'); | |
$('.alternateDefWin').css('display','none'); | |
$( "body" ).prepend( "<div class='alternateDef animated bounce' id='alternateDefId'><p></p><a target='_blank' class='buttonGoogle'>check</a> <i class='fa fa-times' aria-hidden='true' id='crossOffThisDefError'></i></div>" ); | |
$(".alternateDef p").text(errorM); | |
$('#crossOffThisDefError').click(function(){ | |
$('.alternateDef').css('display','none'); | |
}) | |
$('.buttonGoogle').attr('href', resultU); | |
setTimeout(function() { | |
$('.alternateDef').fadeOut('fast'); | |
}, 10000); | |
} | |
else | |
{ // | |
$(".alternateDef").css('display','none'); | |
$(".resultDefinitions").html('').css('background','none'); | |
$(".alternateDefWin p").text(resultA); | |
$(i).each(function(){ | |
console.log('success',xml); | |
$(".resultDefinitions").css('display','block'); | |
$('#texter').prepend('<div class="resultDefinitions animated bounceInLeft"><ul><li>' + $(this).text() +'</li></ul></div>'); | |
$( "body" ).prepend( "<div class='alternateDefWin animated fadeIn' id='alternateDefIdWin'><p>Not satisfied? check this alternate definition.</p><a target='_blank' class='buttonGoogleWin'>Check</a> <i class='fa fa-times' aria-hidden='true' id='crossOffThisDefErrorWin'></i></div>" ); | |
$('.buttonGoogleWin').attr('href', resultU); | |
$('.resultDefinitions').click(function(){ | |
$('#texter').css('display','none'); | |
}) | |
$('#crossOffThisDefErrorWin').click(function(){ | |
$('.alternateDefWin').css('display','none'); | |
$("body").click(function(){ | |
$('.alternateDefWin').css('display','none'); | |
}) | |
}) | |
}); | |
} | |
}, | |
error:function(jqXHR,textStatus,errorThrown ){ | |
alert('Error!:'+errorThrown ); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment