mardi 28 juin 2016

I can't get a value from a var out of a function

I'm trying to get a value from a var inside a function and use that out this function. I'm using tipue search plug in where in live mode i must insert in a var "tipuesearch_pages: []" all the pages that can be use for searching but I want make it dinamically. So first with an Ajax call I get all data of pages from the folder "/pages" on ftp, then I filter the result for getting just the name of the pages and I append it to a div, after I use this text for put it on the "tipuesearch_pages" var, all work fine but at the end the plug in can't read the pages name and give the result.

I think the problem is that I can't get the value out of the function.

JS on my web page:

var lista_html= null
var tipuesearch_pages = [];

function getData() {
 return $.ajax({
    url: 'pages/',
    type: 'GET'
 });
}

function handleData(data /* , textStatus, jqXHR */ ) {
 alert("data: "+data);
 fileExt = ".html";
 $(data).find("a:contains(" + fileExt + ")").each(function () {
     $("#fileNames").append( '"http://www.pizzeriaetoile.com/test_tipue/pages/'+$(this).text()+'"');
 });
 lista_html = $("#fileNames").text();
 alert("lista_html:"+lista_html);
}
tipuesearch_pages = [lista_html];
alert("tipuesearch_pages :"+tipuesearch_pages);

getData().done(handleData);

I've tried also with .push:

var lista_html= null
var tipuesearch_pages = [];

function getData() {
 return $.ajax({
    url: 'pages/',
    type: 'GET'
 });
}

function handleData(data /* , textStatus, jqXHR */ ) {
 alert("data: "+data);
 fileExt = ".html";
 $(data).find("a:contains(" + fileExt + ")").each(function () {
     $("#fileNames").append( '"http://www.pizzeriaetoile.com/test_tipue/pages/'+$(this).text()+'"');
 });
 lista_html = $("#fileNames").text();
 alert("lista_html:"+lista_html);

 tipuesearch_pages.push(lista_html);
}
alert("tipuesearch_pages :"+tipuesearch_pages);

getData().done(handleData);

Test page that try to search the word 'Alpine'

Aucun commentaire:

Enregistrer un commentaire