I have the following script which I need to combine together somehow. Each function works individually at the moment.
// Check if file exists clientside
function FileExists(path) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
FileExist = fso.FileExists(path);
if (FileExist == true){
return true
} else {
return false
}
}
// Get links from database
function getSearchResults() {
var search;
search = $(".txtHeaderSearch").val();
$.ajax({
url: 'results.aspx',
type: 'POST',
data: { strPhrase:search },
success: function(results) {
// Need to somehow stop the .exe links from appearing on screen if FileExists == false
$("#divSearchResults").empty().append(results);
}
});
}
// The returned data looks something like this
<div><a href="link1.xls">link 1</a></div>
<div><a href="link2.exe">link 2</a></div>
<div><a href="link3.doc">link 3</a></div>
<div><a href="link4.aspx">link 4</a></div>
Is it possible to somehow integrate the FileExists function with the ajax success function to prevent the .exe links from appearing on the clients screen if the exe file in question does not exist on the clients computer?
EDIT 1: The following is giving me an object does not support this property or method error:
success: function(results)
{
results.find('a[href$=".exe"]').each(function(){
if (FileExists(this.href)) {
$(this).parent().remove();
}
});
$("#divSearchResults").empty().append(results);
}
EDIT 2: No longer giving the error, but does not remove the non existant exe files either.
success: function(results)
{
$(results).find('a[href$=".exe"]').each(function(){
if (FileExists(this.href)) {
$(this).parent().remove();
}
});
$("#divSearchResults").empty().append(results);
}
EDIT 3: This does not work either.
success: function(results)
{
var $results = $(results);
$results.find('a[href$=".exe"]').each(function(){
if (! FileExists(this.href)) {
$(this).parent().remove();
}
});
$("#divSearchResults").empty().append($results);
}