Cant figure out search error - javascript

working on this, too. I've fixed the spelling and (i think) the bracket errors. Also fixed a couple errors I saw that stood out, but didn't get too far. I'm still stumped as to where to go with it next.
(function(){
// Variable initialization (DO NOT FIX ANY OF THE BELOW VAR's)
var resultsDIV = document.getElementById("results"),
searchInput = document.forms[0].search,
currentSearch = ''
;
// Validates search query
var validate = function(query){
// Trim whitespace from start and end of search query
while (query.charAt[0] === " "){
query = query.substring(1, query.length);
};
while (query.charAt(query.length-1) === ""){
query = query.substring(0, query.length - 1);
};
// Check search length, must have 3 characters
if (query.length < 3){
alert ("Your search query is too small, try again.");
// (DO NOT FIX THE LINE DIRECTLY BELOW)
searchInput.focus();
return;
};
search (query);
};
// Finds search matches
var search = function (query){
// split the user's search query string into an array
var queryArray = query.join(" ");
// array to store matched results from database.js
var results = [];
// loop through each index of db array
for(var i=0, j=db.length; i<j; i++){
// each db[i] is a single video item, each title ends with a pipe "|"
// save a lowercase variable of the video title
var dbTitleEnd = db[i].indexOf('|');
var dbitem = db[i].tolowercase().substring(0, dbTitleEnd);
// loop through the user's search query words
// save a lowercase variable of the search keyword
for(var ii=0, jj=queryArray.length; ii<jj; ii++){
var qitem = queryArray[ii].tolowercase();
// is the keyword anywhere in the video title?
// If a match is found, push full db[i] into results array
var compare = dbitem.indexOf(qitem);
if(compare !== -1){
results.push(db[i]);
};
};
};
};
results.sort();
// Check that matches were found, and run output functions
if(results.length = );{
noMatch();
}else{
showMatches(results);
};
// Put "No Results" message into page (DO NOT FIX THE HTML VAR NOR THE innerHTML)
var noMatch = function(){
var html = ''+
'<p>No Results found.</p>'+
'<p style="font-size:10px;">Try searching for "JavaScript". Just an idea.</p>'
;
resultsDIV.innerHTML = html;
};
// Put matches into page as paragraphs with anchors
var showMatches = function(results){
// THE NEXT 4 LINES ARE CORRECT.
var html = '<p>Results</p>',
title,
url
;
// loop through all the results search() function
for(var i=0, j=results.length; i<j; i++){
// title of video ends with pipe
// pull the title's string using index numbers
titleEnd = results[i].indexOf('|');
title = results[i].subString(0, titleEnd);
// pull the video url after the title
url = results[i].substring(results[i].indexOf('|')+1, results[i].length);
// make the video link - THE NEXT LINE IS CORRECT.
html += '<p><a href=' + url + '>' + title + '</a></p>';
};
resultsDIV.innerHTML = html; //THIS LINE IS CORRECT.
};
// The onsubmit event will be reviewed in upcoming Course Material.
// THE LINE DIRECTLY BELOW IS CORRECT
document.forms[0].onsubmit = function(){
var query = searchInput.value;
validqte(query);
// return false is needed for most events - this will be reviewed in upcoming course material
// THE LINE DIRECTLY BELOW IS CORRECT
return false;
;
})();

There are a few syntax errors which should prevent this from running at all.
// syntax error
var results = ();
// should be
var results = [];
Also it appears that the definition of showMatches ends with an unmatched right parenthesis.
Given that somehow the code is running with these syntax errors (and possibly others I didn't notice), the (a) glaring issue is:
if (results.length = 0) {
Yeah, that's an assignment, not a comparison. You just set the array's length to zero, which effectively clears it. This both fails the length test here (returning 0), then hands the now empty array off to be displayed as results, but of course it's now an array of length 0.
On another note, probably not an actual problem but I can't help it. The while loop business to trim the string? Yeah, don't do that. Use trim. If you need a polyfill there's always:
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}

Related

Google App Script to Break out Query Parameters in Google Sheets

I have a Google Sheet with 100 https request URLs with query parameters. The URLs look like this:
https://122.2o7.net/b/ss/ryan1/1/JS-2.0.0/s12345678?AQB=1&ndh=1&pf=1&t=6%2F9%2F2018%208%3A48%3A34%206%20360&ts=1538837314190&vid=test&fid=1w23232-erwwwre&ce=UTF-8&ns=ryan&pageName=ryan%3Atest%3Apage&g=https%3A%2F%2Fryanpraski.com%2F&cc=USD&ch=home&events=event1&c1=D%3Dv1&v1=evar1value&h1=hier1value&v20=evar20value&bh=8&AQE=1
I want to use Google App Script to break out the query parameters and put them neatly into the Google Sheet like this:
I got as far as the code below to break query string and split the query string parameters by the & delimiter, but I am not sure what to do next.
A couple cases that I need to consider as well.
There could be URLs with more or fewer parameters than my sample URL, but there will always be some overlay. I want to have the column headers automatically update.
There could be values like c1=D%3Dv1 where the decoded value is c1=D=v1
Any help would be greatly appreciated!
function test() {
var url = "https://122.2o7.net/b/ss/ryan1/1/JS-2.0.0/s12345678?AQB=1&ndh=1&pf=1&t=6%2F9%2F2018%208%3A48%3A34%206%20360&ts=1538837314190&vid=test&fid=1w23232-erwwwre&ce=UTF-8&ns=ryan&pageName=ryan%3Atest%3Apage&g=https%3A%2F%2Fryanpraski.com%2F&cc=USD&ch=home&events=event1&c1=D%3Dv1&v1=evar1value&h1=hier1value&v20=evar20value&bh=8&AQE=1";
var cleanUrl = decodeURIComponent(url);
var params = cleanUrl.split('?')[1];
var s = params;
var t = s.split('&');
var output = [];
t.forEach(function(q) {
output.push([q]);
});
Logger.log(output);
}
The following code breaks out the query parameters and puts them into a specific sheet. It also addresses a couple of possible scenarios:
1 There is no match for an existing code. In that case, a space is entered as a place holder.
2 The URL includes codes not included in the existing list. In that case, the "new" code(s) are added to the list, and their values are recorded also.
3 As the questioner pointed out, some URL parameters include multiple "Equals" signs ("="). Split can't be used in this case because though a parameter can be used to limit the number of split found, the left-over text is not returned in the new array. So I used indexOf (which returned the index of the first occurrence of searchValue) and subString to calculate the two parts of the URL component.
I assumed that the existing list of codes was in Row1, so I created a NamedRange to be able to manage them. If the code finds URL parameters that don't find a match with the codes in the Named Range, then the NamedRange is deleted and re-created to include the "new" codes.
The code outputs results to the "third sheet" (ss.getSheets()2;) in the spreadsheet; this is something that can be changed.
The last row containing data is determined, and the results of the analysis are set in the following row
Note: the url is hard coded.
function so_52825789() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var datasheet = ss.getSheets()[2];
// the codes are in Row1 in a Named Range called DataVariables
var urlvars = ss.getRangeByName('DataVariables').getValues();
// get the number of Columns for the Named Range
var datalen = urlvars[0].filter(String).length;
//Logger.log("Number of columns of codes = "+datalen); //DEBUG
//get the last row of containing data
var lastvals = ss.getRange("A1:A").getValues();
var lastrow = lastvals.filter(String).length;
//Logger.log("the last row is "+lastrow);// DEBUG
// Get the url
var url = "https://122.2o7.net/b/ss/ryan1/1/JS-2.0.0/s12345678?AQB=1&ndh=1&pf=1&t=6%2F9%2F2018%208%3A48%3A34%206%20360&ts=1538837314190&vid=test&fid=1w23232-erwwwre&ce=UTF-8&ns=ryan&pageName=ryan%3Atest%3Apage&g=https%3A%2F%2Fryanpraski.com%2F&cc=USD&ch=home&events=event1&c1=D%3Dv1&v1=evar1value&h1=hier1value&v20=evar20value&bh=8&AQE=1&ejb=1";
// Javascript function to remove the URL encoded charaters
var cleanUrl = decodeURIComponent(url);
// get the URL variables (the second half of the split)
var params = cleanUrl.split('?')[1];
var s = params;
// get the specific query variables by spliting on "&"
var t = s.split('&');
// get the number of query variables
var tlen = t.filter(String).length;
// setup some variables for use later
var output = [];
var mismatchcode = [];
var mismatchdata = [];
var tcount = [];
var nomatch = 0;
var ttest = 0;
var ztest = 0;
// Loop through the known codes from the Named Range
for (i = 0; i < datalen; i++) {
// set a variable value so that you can count how many named codes were found
ttest = 1;
// Start a loop though the query variables in the URL
for (z = 0; z < tlen; z++) {
// get the position of the Equals sign "="; there may be more than one but we only want the first one.
var n = t[z].indexOf("=");
if (n > 0) {
//var result="Equals appears at position = "+(n+1)+" (actual value = "+n+")";
//Logger.log(result);
//get the length of the element
var nstr = t[z].length;
//Logger.log("Length = "+nstr); //DEBUG
// break the element into two halves. The first half is the "Code" and the second half is the "value"
var code = t[z].substring(0, n);
var codevalue = t[z].substring((n + 1), nstr);
//Logger.log("z = "+z+", code is = "+code+", and the value is "+codevalue); // DEBUF
}
// test to whether there is a match between the Named Range Code and the URL
if (urlvars[0][i] == code) {
// set the variable to note a match was detected.
ttest = 0;
// push the code value into an array
output.push(codevalue);
// push the Named range code ID onto an array
tcount.push(z);
//Logger.log("Match "+urlvars[0][i]+" = "+code); //DEBUG
}
} // end of the URL variables loop
// having looped through the URL variables, test to see whether there was a match
// if not (ttest still equals One) then put an empty string in the output array, so ensure that every code has a value
// and keep count of the number of "nomatches"
if (ttest == 1) {
output.push(" ");
Logger.log("No match for " + urlvars[0][i]);
nomatch = nomatch + 1;
}
} // end of the Named Range loop
// create an array for 2d format
var outeroutput = [];
// put the loop array into the blank array. The result is a 2d array that can be read by the Google sheets script.
outeroutput.push(output);
// For the NamedRange analysis, we can now set the values from the loop
var targetrange = datasheet.getRange(lastrow + 1, 1, 1, datalen);
targetrange.setValues(outeroutput);
//Logger.log("targetrange = "+targetrange.getA1Notation()); //DEBUG
// count how matches were found for URL variables
var tcountlen = tcount.filter(String).length;
// compare the number of variables in the URL with the number of matches.
// If there is a difference, then we need to loop through the URL variables, find the ones that didn't match and do stuff with them.
if ((tlen - tcountlen) > 0) {
// starp loop for URL variables
for (z = 0; z < tlen; z++) {
// set the variable to detect whether or not a a match was made.
ztest = 1;
// Repeat the process of splitting the component code and value
var n = t[z].indexOf("=");
if (n > 0) {
// get the length of the variable
var nstr = t[z].length;
// get the componet parts
var code = t[z].substring(0, n);
var codevalue = t[z].substring((n + 1), nstr);
//Logger.log("z = "+z+", code is = "+code+", and the value is "+codevalue); //DEBUG
}
// start the loop for thecodes in the NamedRange
for (i = 0; i < datalen; i++) {
// If there's a match, chnage the value of the 'match testing' varuable
if (urlvars[0][i] == code) {
ztest = 0;
}
} // end of the loop for NamedRange codes
// if there hasn't been match, then
// push the url variable code and value onto some respective arrays
if (ztest == 1) {
mismatchcode.push(code);
mismatchdata.push(codevalue);
}
} // end of the URL variables loop
//Logger.log("Code fields = "+datalen+", data fields = "+tlen);// DEBUG
//Logger.log("Total no-matches for codes = "+nomatch); // DEBUG
// Logger.log("Total no-matches for URL fields = "+(tlen-tcountlen)); //DEBUG
// So, what shall we do if there the number of variables in the NAMED RANGE does equal the number of variables
// if((tlen-tcountlen) !=0){
// These rows are just for DEBUG assignstance.
// for (i=0;i<(tlen-tcountlen);i++){ //DEBUG
// Logger.log("URL field not found: code = "+mismatchcode[i]+", value = "+mismatchdata[i]); //DEBUG
// } //DEBUG
// create the arrays to act as 2d
var outermismatchcode = [];
var outermismatchdata = [];
// Push the mismatch arrays to the create the 2d arrays
outermismatchcode.push(mismatchcode);
outermismatchdata.push(mismatchdata);
// Identify the range for the addition URL Codes and values
// set the respective values
var extraurlcoderange = datasheet.getRange(1, datalen + 1, 1, (tlen - tcountlen));
extraurlcoderange.setValues(outermismatchcode);
var extraurldatarange = datasheet.getRange(lastrow + 1, datalen + 1, 1, (tlen - tcountlen));
extraurldatarange.setValues(outermismatchdata);
// We want to add the "new" codes found in the URL to the Named Range.
// Start by deletinging the existing NamedRange
ss.removeNamedRange("DataVariables");
// Define the parmeters for a new range.
// The main thing is that we need to add more columns
var newnamedrange = datasheet.getRange(1, 1, 1, (datalen + (tlen - tcountlen)))
// So, Create a new NamedRange using the same name as before.
ss.setNamedRange('DataVariables', newnamedrange);
// The following lines are just to check that everything worked OK
// var rangeCheck = ss.getRangeByName("DataVariables"); // DEBUG
// if (rangeCheck != null) { //DEBUG
// Logger.log("Columns in the new named range = "+rangeCheck.getNumColumns());//DEBUG
// } ,//DEBUG
// var rangeCheckName = rangeCheck.getA1Notation(); //DEBUG
// Logger.log("the new named range is = "+rangeCheckName);//DEBUG
} // end of the loop to identify URL variables that didn't match a code in the NamedRange
}
Note the addition value of the c1 code includes the relevant equals sign. Also the URL includes an additional parameter ("ejb=1") that is not in the existing list; this code and its value are added to the spreadsheet, and the NamedRange now includes the "new" code.

conditional splitting of string and word by matching characters in js

I have very long string that contains html as a string , numbers , my special bindings and numbers I want to split my bindings and sentences with spaces separately but my program is separately my bindings and words .
my js code:-
var x = 'hey this is {{name}} and I love to {{write}} and to learn as
much as I can. Now I am trying to separate sentences and my bindings'
var c = x.match(/\s*\S*\s*/g) // this splits words from string including
space
var mt = x.match(/{(.*)}/g); // trying to take out bindings but this don't
work
mt.forEach(function(a){ // taking each bindings separately
var z = x.match(a)
})
console.log(mt)
Somthing like this .. but I know this is totally wrong please help me
I don't have any idea :-
output that I am expecting:-
(5) ["hey this is", "i", "{{name}}", " and I love to ", "{{write}}", " and to learn as ↵ much as I can. Now I am trying to separate sentences and my bindings"]
How can i do this?
Please don't use jquery
Try this:
I've commented my code hoping it would make it easier to read. But do note that this code is far from perfect although it solves your problem.
var rawString = 'hey this is {{name}} and I love to {{write}} and to learn as much as I can. Now I am trying to separate sentences and my bindings';
var arrayRawString = rawString.match(/\s*\S*\s*/g); // this splits words from string including space
var arrayPlaceholder = rawString.match(/{(.\S*)}+/g); // trying to take out bindings but this don't work
// to store the final output
var separedArray = [];
// keeping track of the index to stich the array up
var posStart = 0;
var posEnd = 0;
arrayPlaceholder.forEach(function(arg){ // taking each bindings separately
// length of the array that holds placeholder (bindings)
var arsLength = arrayRawString.length;
for(var i = 0; i < arsLength; ++i) {
// if the provided text matches the original array's element
if(arrayRawString[i].match(arg)){
// to store the index
posEnd = arrayRawString.indexOf(arrayRawString[i]);
// join the pieces together upto the index defined
var res = arrayRawString.slice(posStart, posEnd).join('');
// to indicate whether the stored string is the placeholder
var flag = true;
// store the string obtained
separedArray.push(res.replace(arrayPlaceholder[(arrayPlaceholder.indexOf(arg) - 1) < 0 ? 0 : arrayPlaceholder.indexOf(arg) - 1 ], ''));
// check if the string still has placeholder (bindings)
// to remove it
for(var j = 0; j < arg.length; ++j) {
if(res[j] !== arg[j]) {
flag = !flag;
}
}
if ( flag ) {
separedArray.push(arg);
}
// last end position is the start position for next round
posStart = posEnd;
// because the loop runs only arrayPlaceholder.length times
// it solves the problem of last part not getting pushed to the final array
if( arrayPlaceholder[arrayPlaceholder.length-1] === arg ) {
res = arrayRawString.slice(posStart, arrayRawString.length).join('');
separedArray.push(res.replace(arg, ''));
}
}
}
});
console.log(separedArray);

Function to return a list - issue with filtering the output

I am trying to output only articles if authorsId = authorId.
Beside that the whole function works exactly as I want, here it is:
The general idea is to limit access to only own articles.
So, my question is: how do I limit the results to show only articles written by the owner of the page we are on (authorsId = authorId).
function ArticlesListReturn(returned) {
xml = returned.documentElement;
var rel = document.getElementById('related_category_articles');
rel.options.length = 0;
var status = getXMLData('status');
var title = '';
var id = '';
var authorid = '';
if (status == 0) {
alert("%%LNG_jsArticleListError%%" + errormsg);
} else {
var authorid = document.getElementById("authorid").value; // Serge
// authorsid = getNextXMLData('authors',x);
for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) {
title = getNextXMLData('titles', x);
id = getNextXMLData('ids', x);
authorsid = getNextXMLData('authors', x);
alert(authorsid) // authors of each article - it returns the proper values
alert(authorid) // author of the page we are on - it returns the proper value
var count = 0;
rel.options[x] = new Option(title, id, authorid); // lign that returns results
title = '';
id = '';
authorid = '';
}
}
I suspect the problem is when you try performing a conditional statement (if/then/else) that you are comparing a number to a string (or a string to a number). This is like comparing if (1 == "1" ) for example (note the double quotes is only on one side because the left would be numeric, the right side of the equation would be a string).
I added a test which should force both values to be strings, then compares them. If it still gives you problems, make sure there are no spaces/tabs added to one variable, but missing in the other variable.
Also, I changed your "alert" to output to the console (CTRL+SHIFT+J if you are using firefox). The problem using alert is sometimes remote data is not available when needed but your alert button creates a pause while the data is being read. So... if you use alert, your code works, then you remove alert, your code could reveal new errors (since remote data was not served on time). It may not be an issue now, but could be an issue for you going forward.
Best of luck!
function ArticlesListReturn(returned) {
xml = returned.documentElement;
var rel = document.getElementById('related_category_articles');
rel.options.length = 0;
var status = getXMLData('status');
var title = '';
var id = '';
var authorid = '';
if (status == 0) {
alert("%%LNG_jsArticleListError%%" + errormsg);
} else {
var authorid = document.getElementById("authorid").value; // Serge
// authorsid = getNextXMLData('authors',x);
for (var x = 0; x < xml.getElementsByTagName('titles').length; x++) {
title = getNextXMLData('titles', x);
id = getNextXMLData('ids', x);
authorsid = getNextXMLData('authors', x);
console.log("authorsid = "+authorsid); // authors of each article - it returns the proper values
console.log("authorid = "+authorid); // author of the page we are on - it returns the proper value
if( authorsid.toString() == authorid.toString() )
{
rel.options
var count = 0;
console.log( authorsid.toString()+" equals "+authorid.toString() );
rel.options[rel.options.length] = new Option(title, id, authorid); // lign that returns results
}
else
{
console.log( authorsid.toString()+" NOT equals "+authorid.toString() );
}
title = '';
id = '';
authorid = '';
}
Did you check the console for messages? Did it correctly show authorid and authorsid?
I have edited the script and made a couple of additions...
The console will tell you if the conditional check worked or not (meaning you will get a message for each record). See the "if/else" and the extra "console.log" parts I added?
rel.options[x] changed to equal rel.options[rel.options.length]. I am curious on why you set rel.options.length=0 when I would instead have done rel.options=new Array();

unable to search for array items

In this program I am unable to search for items in an array. The db variable is already defined. I am unable to get into the search function and run it. Just curious why this might be happening. I am able to run the first validate function but then stops and will not perform the rest of the code.
// Create privatized scope using a self-executing function
(function() {
console.log("hello");
// Variable initialization (DO NOT FIX ANY OF THE BELOW VAR's)
var resultsDIV = document.getElementById("results"),
searchInput = document.forms[0].search,
currentSearch = '';
// Validates search query
var validate = function (query) {
console.log("validate");
// Trim whitespace from start and end of search query
query = query.trim();
// Check search length, must have 3 characters
if (query.length < 3) {
alert("Your search query is too small, try again.");
}else{
search(query);
// (DO NOT FIX THE LINE DIRECTLY BELOW)
searchInput.focus();
}
console.log("test");
};
console.log("outside search function");
// Finds search matches
var search = function (query) {
console.log("In search function");
// split the user's search query string into an array
var queryArray = query.split(" ");
// array to store matched results from database.js
var results = [];
// loop through each index of db array
for (var i = 0, j = db.length; i < j; i++) {
console.log(i);
// each db[i] is a single video item, each title ends with a pipe "|"
// save a lowercase variable of the video title
var dbTitleEnd = db[i].indexOf('|');
var dbItems = db[i].toLowerCase().substring(0, dbTitleEnd);
}
// loop through the user's search query words
// save a lowercase variable of the search keyword
for (var ii = 0, jj = queryArray.length; ii < jj; ii++) {
var qItem = queryArray[ii].toLowerCase();
}
// is the keyword anywhere in the video title?
// If a match is found, push full db[i] into results array
var compare = dbItems.indexOf(qItem);
if (compare !== -1) {
results = results.push(db[i]);
}
results.sort();
// Check that matches were found, and run output functions
if (results.length === 0) {
noMatch();
} else {
showMatches(results);
}
};
// Put "No Results" message into page (DO NOT FIX THE HTML VAR NOR THE innerHTML)
var noMatch = function() {
var html = '' +
'<p>No Results found.</p>' +
'<p style="font-size:10px;">Try searching for "JavaScript". Just an idea.</p>'
;
resultsDIV.innerHTML = html;
};
// Put matches into page as paragraphs with anchors
var showMatches = function (results) {
// THE NEXT 4 LINES ARE CORRECT.
var html = '<p>Results</p>',
title,
url
;
// loop through all the results search() function
for (var i = 0, j = results.length; i < j; i++) {
// title of video ends with pipe
// pull the title's string using index numbers
var titleEnd = results[i].indexOf('|');
title = results[i].subString(0, titleEnd);
// pull the video url after the title
url = results[i].substring(results[i].indexOf('|') + 1, results[i].length);
// make the video link - THE NEXT LINE IS CORRECT.
html += '<p><a href=' + url + '>' + title + '</a></p>';
resultsDIV.innerHTML = html; //THIS LINE IS CORRECT.
}
};
console.log("start of program");
/***** start of program *******/
// The onsubmit event will be reviewed in upcoming Course Material.
// THE LINE DIRECTLY BELOW IS CORRECT
document.forms[0].onsubmit = function(){
var query = searchInput.value;
validate(query);
// return false is needed for most events - this will be reviewed in upcoming course material
// THE LINE DIRECTLY BELOW IS CORRECT
return false;
};
})();
I checked your code and everything is there but somehow all wrongly connected. It seems that you have completely wrong concept about for loops. I did several minor changes:
query is trimmed on reading because we need it not just in validator
validator:
if query is too short set focus back
it validated but also allowed operation to continue in case of error - changed
changed to isValid() and checked in onsubmit handler
search:
wrong concept of for loops
you retrieve dbTitleEnd/dbItems and than overwrite them by the next one
there is no need to do results = results.push(db[i]); but just `results.push(db[i]);
subString() corrected to substring()
console.log() messages are left in
See Example in jsFiddle.

for loop randomly crashing on certain iterations

there must be someone out there who can spot my error! I'm parsing an xml feed and running through a loop to get certain items on the page:
www.highlandradio.com/feed/
I am encountering an error on the line where I am setting the variable mp3Test. All I want to do is to set it equal to the url attribute of the enclosure tag (if it exists). This is why I am including the test to check if each item has an enclosure tag, if it does I am setting it to be equal to the url tag but if it doesn't I am setting it to be equal to 'null.mp3' for now. When running through the loop I am getting the correct numbers for a certain amount and then on some iterations of the loop, i crashes. I am using titanium and so am not getting any proper error messages. It simply highlights the line where I set up mp3Test and says to debug the current instruction pointer. I'm going crazy with this. Please help.
var url="http://www.highlandradio.com/feed/";
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function() {
// Data is returned from the blog, start parsing
var doc = this.responseXML.documentElement;
// begin looping through blog posts
var items = doc.getElementsByTagName("item");
var enclosure = doc.getElementsByTagName("enclosure");
console.log("NEWEST Enclosure Length -----------"+enclosure.length);
for (var i = 0; i < items.length; i++) {
var itemsEnclosureLength=items.item(i).getElementsByTagName("enclosure").length;
var itemsEnclosureExists=items.item(i).getElementsByTagName("enclosure");
console.log("EXISTs ----------------------------------"+ itemsEnclosureExists);
console.log("Newest ENCLOSURE LENGTH ---------------------- [ "+i+ " ]" + itemsEnclosureLength);
var str=items.item(i).getElementsByTagName("content:encoded").item(0).text;
console.log("STRING TEST "+ str);
var patt1=/([a-z\-_0-9\/\:\.]*\.(jpg|jpeg|png|gif))/i;
var urlMatch = str.match(patt1);
console.log("MATCHES TESTER -----------------------------------" + typeof urlMatch);
data.push({
title: items.item(i).getElementsByTagName("title").item(0).text,
leftImage: str.match(patt1) !== null ? str.match(patt1)[0] : 'image_news.png',
dataToPass: items.item(i).getElementsByTagName("description").item(0).text,
mp3Test: itemsEnclosureLength > 0 ? items.item(i).getElementsByTagName("enclosure").item(i).getAttribute("url"):'NULL.MP3'
});
console.log("Check this --------------------------"+ mp3Test);
}
BUMP2
The problem was that I was using two counters which had the same value! Silly mistake but...Here's the solution:
var itemsEnclosureLength=items.item(i).getElementsByTagName("enclosure").length;
var itemsEnclosureExists=items.item(i).getElementsByTagName("enclosure");
if (itemsEnclosureExists){
for (n=0; n<itemsEnclosureLength; n++){
mp3=items.item(i).getElementsByTagName("enclosure").item(n).getAttribute("url");
}
}
else{
mp3= 'NULL.mp3';
}

Categories

Resources