www.webdeveloper.com
Results 1 to 6 of 6

Thread: please help me simplifying my code ...

  1. #1
    Join Date
    Dec 2008
    Posts
    12

    please help me simplifying my code ...

    Hi (I am not a native-English speaker),

    I prepared a RSS generator to display information on the online journal I am editing

    I am not familiar with javascript but I managed to get something that works

    I would like to control the number of rows visible with respect to the "authors" item

    1 row visible if only one author, 2 for 2, 3 for 3 ... with a tool to select the number of rows to display.

    the problem is that the display of the generated list of authors should vary depending on the number of authors

    author1
    author1 & author2
    author1, author2 & author3
    author1, author2, author3 & author4

    May be it can be done with a do while (I used a set of if else which limited me to 6 "authors" rows always visible)

    http://paleopolis.rediris.es/cg/generator_RSS2.html

    There should be a "smart" way ...

    Thank you in advance for your appreciated help,
    Bruno

  2. #2
    Join Date
    Oct 2008
    Posts
    349
    The following code loops from 1 thru N, add the authors to an array. It then join the array using the join() function, then replace the last comma with a ampersand using RegExp.

    Don't think it's more efficient. Might be cleaner

    Code:
    ...
    	txt+='  <summary>';
    
    	var authorlist = new Array();
    	for (var i = 1; i <= form.auteurs.options[form.auteurs.selectedIndex].text; i ++)
    	{
    		authorlist.push(form['author' + i + 'B'].value + ' ' + form['author' + i + 'C'].value + ' ' + form['author' + i + 'A'].value);
    	}
    
    	var authors = authorlist.join(', ').replace(/,([^,]*)$/, ' \&$1');
    
    	txt+= authors + '.- ' + form.title.value + '.- ' + form.abstract.value + '.- SRef-ID: http://overview.sref.org/1634-0744/CG' + form.year.options[form.year.selectedIndex].text + '_' + cat + form.issue.value + '.- Handle-ID: http://hdl.handle.net/2042/' + form.handle.value + '</summary>\r\n';
    	txt+='</entry>\r\n';
    	
    	txt+='\r\n';
    ...

  3. #3
    Join Date
    Dec 2008
    Posts
    12

    I couldn't have find it myself

    Many thanks: it is really amazing how it works fine!

    I only change the & for a & as I want it to be understood by a RSS feed (a single & will stack it)

    Can you help me with the 2nd part of my quthe estion?
    that is how to select or better to enter a number of authors that would generate one data entry line for each (the default being 1 author, i.e. 1st author, = 1 line)?

    Can I ask one more thing to even get a better result!?...

    there are many "diacritic signs" in Spanish, French, Italian, ...
    and I would like to get a "clean" final text, that is without ... but with codes instead ...

    I suppose I have to insert something before the final

    form.result.value=txt;}

  4. #4
    Join Date
    Dec 2008
    Posts
    12

    I couldn't have find it myself

    Many thanks: it is really amazing how it works fine!

    I only change the & for a & as I want it to be understood by a RSS feed (a single & will stack it)

    I posted the new version under
    http://paleopolis.rediris.es/cg/generator_RSS2VV.html

    Can you help me with the 2nd part of my question?
    that is how to select or better to enter a number of authors that would generate one data entry line for each (the default being 1 author, i.e. 1st author, = 1 line)?

    Can I ask one more thing to even get a better result!?...

    there are many "diacritic signs" in Spanish, French, Italian, ...
    and I would like to get a "clean" final text, that is without ... but with codes instead ...

    I suppose I have to insert something before the final

    form.result.value=txt;}

  5. #5
    Join Date
    Oct 2008
    Posts
    349
    For diacritics, use this function, pass any string to it, it will convert all characters that is not English-word character or space to HTML entity (number-based).
    Code:
    function htmlentities(s)
    {
    	return s.replace(/[^\w ]/g, function (s) {return '&#' + s.charCodeAt(0) + ';'; });
    }
    For the first one, you can probably replace what is inside the FOR-loop with:
    Code:
    if (form['author' + i + 'B'].value || form['author' + i + 'C'].value || form['author' + i + 'A'].value)
    	authorlist.push(form['author' + i + 'B'].value + ' ' + form['author' + i + 'C'].value + ' ' + form['author' + i + 'A'].value);

  6. #6
    Join Date
    Dec 2008
    Posts
    12
    Regarding the diacritic signs ... unfortunately the code was interpreted and for instance in my sentence the &#038;#233; were converted into &#233;
    it should have read
    there are many "diacritic signs" in Spanish, French, Italian, ...
    and I would like to get a "clean" final text, that is without &#233; &#224; &#231; ... but with &#038;#233; &#038;#224; &#038;#231; codes instead

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles