views:

82

answers:

3

Hi,

Using jquery I am doing something like:

$("#someId").append("this is some text, go here.");

How can I insert a url like this safely? Is there an easier way to escape all those characters that will cause js to break?

+1  A: 

I think escape should do it:

$("#someId").append(escape('this is some text, go here'));
karim79
But only for the URL portion.. $('#someId').append('this is some text, go <a href="'+escape('messy url')+'">here</a>');
Al
A: 

Try using the html function, jquery docs

I'm no expert but something like this should work

$("#someId").html("<span>this is some text, go <a href='http://google.com'&gt;here&lt;/a&gt;&lt;/span&gt;");
Simon Fox
A: 

Unless I'm mistaken by what you are asking, you could do something like

link = $('<a>here</a>');
link.attr('href', 'http://www.google.com');
$("#someId").append("this is some text, go " + link);
theIV
The + link results in: [object Object]
mrblah
You're correct. Thanks for pointing that out.You'd have to do something along the lines of: $("#someId").append("this is some text, go ") .append(link) .append(".");That starts to get kind ugly. Especially adding another append just for the ".". Sorry about that.
theIV
@homestead @theIV - you would just need to do $("#someId").append("this is some text, go " + link.html());
karim79
@karim79—I tried that as well. That only yields the text from the link, not the full html element.
theIV