Edit returned results before appending on screen
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 .e开发者_运维问答xe 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);
}
you could do this, attribute ends with selector
$(results).find('a[href$=".exe"]').parent().remove();
based on my experienced, sometimes that line would not work. If it too happens to you, do this,
var results = $('<div>').html(results).find('a[href$=".exe"]').parent().remove();
results = results.html();
then your result will now have something line this,
<div><a href="link1.xls">link 1</a></div>
<div><a href="link3.doc">link 3</a></div>
<div><a href="link4.aspx">link 4</a></div>
updated for the comment below.
$(results).find('a[href$=".exe"]').each(function(){
if (! FileExists(this.href)) { // uses href as the path...
$(this).parent().remove();
}
});
The answer was to include a span in each line. For some reason that works...
精彩评论