1

In javascript I have an array like

psi=["www.google.com","www.facebook.com","www.collegehumor.com"]

I want to turn it to a formatted string like

var final=<a href="www.google.com" target="_blank">www.google.com</a></br>,<a href="www.facebook.com" target="_blank">www.facebook.com</a></br>,<a href="www.collegehumor.com" target="_blank">www.collegehumor.com</a></br>

so I can send from server to client something like

//websockets, btw...
connection.send('Search Results'+t_name+'</br>Links'+final+'</br>');

and it will render just the working links.

Something like

www.google.com
www.facebook.com
www.collegehumor.com

I know there is the toString(). But no luck with that. I cant make it create working links.

I tried to implement a for inside the connection.send but Dreamweaver says there is a syntax error. That's why I try to convert the array to formatted string.

Any advise?

Thanks

7
  • Sounds like a job for a template engine (my personal favorite is doT because it's fast, it generates efficient templates, and it's tiny). Commented Aug 5, 2013 at 14:46
  • Can you not build the string in a loop using += and then pass it? Commented Aug 5, 2013 at 14:47
  • Add "http://" before www, otherwise it will fail. Also, when declaring the variable "final", as it is a string, it needs to be between quotes and escape those quotes that are form the text itself: var final="<a href=\"http://www.google.com\" target=\"_blank\">www.google.com</a>, ..."; Commented Aug 5, 2013 at 14:48
  • @AlejandroIván Alternatively, you can avoid the escaping of " by simply wrapping the enter final string value with '. Commented Aug 5, 2013 at 14:50
  • @ajp15243 true. I'm just used to it :) Commented Aug 5, 2013 at 14:51

3 Answers 3

2
var psi=["www.google.com","www.facebook.com","www.collegehumor.com"]
var str = '';
for(var i in psi)
{
    str += ',<a href="http://'+psi[i]+'" target="_blank">'+psi[i]+'</a><br>';
}
str = str.substr(1);

Alternate version using the string.link() method mentioned by Zim84 in the comments above:

var psi=["www.google.com","www.facebook.com","www.collegehumor.com"]
var str = '';
for(var i in psi)
{
    var url = psi[i];
    str += ','+url.link('http://'+url)+'<br>';
}
str = str.substr(1).replace('">','" target="_blank">');
Sign up to request clarification or add additional context in comments.

Comments

1
var final = "";
for (var i = 0; i < psi.length; i++)
{
    var linkWithHttp = "http://" + psi[i];
    final += "<a href=\"" + linkWithHttp + "\" target=\"_blank\">" + psi[i] + "</a><br />,";
}

// Remove the last "," if you added, at least, one element
if (psi.length > 0)
{
    final = final.substring(0, final.length - 1);
}

// Now final is like what you want
// Send it using connection.send()

Comments

0

If you are willing to use jQuery it's a piece of cake:

var final = $.map(psi, function(link) {
  return '<a href="' + link + '" target="_blank">' + link + '</a>';
}).join('<br />,');

4 Comments

ECMAScript5's Array.prototype.map would take exactly the same function.
Paul S is correct. If you don't care about IE 8 that would work, but you'd have to do the join manually (it wouldn't be hard, though).
what do you mean "have to do the join manually? Array's join has been part of JavaScript since ECMAScript 1st Edition - pre IE6
I didn't see it in the documentation, but I guess it was only showing the NEW stuff in ECMAScript5 on the page I was viewing. I've gotten so used to using jQuery for everything that I don't even know what's in the language itself!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.