views:

31

answers:

2

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);
    }
A: 

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();
    }
});
Reigel
I don't want to remove all the .exe files from the results, only the .exe files which do not exist on the clients computer. Hence the FileExists function.
oshirowanen
edited my answer... ;)
Reigel
Does that updated script go into the success section of the jquery ajax function?
oshirowanen
yes, before this line - `$("#divSearchResults").empty().append(results);`
Reigel
It's giving me en object does not support this property or method error. I've updated the original post to show a snippet of the edited script.
oshirowanen
ahh try `$(results)` for `result.find()`.
Reigel
It's no longer giving the error, but it's not removing the non existant exe link and parent div. Updated original question to show current script.
oshirowanen
change `(FileExists(this.href))` to `(!FileExists(this.href))`. noticed the `!` there. ;)
Reigel
Still not working. I've even tried EDIT 3 in the original post which does not work either. I.E. it keeps displaying exe files which do not exist on the clients computer. It should be removing those from the browser window by making use of FileExists.
oshirowanen
A: 

The answer was to include a span in each line. For some reason that works...

oshirowanen