I have one variable with some text and anchor tag element like
var success_msg = 'Please <a href="javascript:void(0);" id="popup-login" class="login" title="Login">login</a> to save your wishlist.';
Now this variable I am passing to ajax and this message display with popup on ajax success.
Ajax Code:
var success_msg = 'Please <a href="javascript:void(0);" id="popup-login" class="login" title="Login">login</a>+ to save your wishlist.';
$.ajax({
type: "POST",
url: BASE_URL+"home/featherlightInfo",
data: "success_msg="+success_msg,
success: function (html) {
$('#loading-div').hide(); // Hide Ajax loader
$('#featherlightInfoPop').html(html);
$.featherlight('#featherlightInfo');
setTimeout(function(){
$(".featherlight-close").trigger("click");
}, 5000);
$('#feathre').addClass('login-feathre');
}
});
Everythig is working perfect but in popup HTML I am getting anchor tag like this:
<a title="\"Login\"" class="\"login\"" id="\"popup-login\"" href="\"javascript:void(0);\"">login</a>`
One of the strange thing is that this code is working perfect from my local server.
I want this result: Please <a href="javascript:void(0);" id="popup-login" class="login" title="Login">login</a> to save your wishlist.
So what to do to get proper anchor element?
Try encodeURI
function of javascript:
var success_msg = 'Please <a href="javascript:void(0);" id="popup-login" class="login" title="Login">login</a>+ to save your wishlist.';
var encode_msg = encodeURI(success_msg);
OR
You can pass a flag message in the ajax request and on the basis of that you can create this 'Please <a href="javascript:void(0);" id="popup-login" class="login" title="Login">login</a>+ to save your wishlist.';
HTML code on server side. This will be good practice.
According to jquery ajax
: When you sent data to the server, it will be converted into a query string, so the quotes is escaped.
The data option can contain either a query string of the form key1=value1&key2=value2, or an object of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false. The processing might be undesirable if you wish to send an XML object to the server; in this case, change the contentType option from application/x-www-form-urlencoded to a more appropriate MIME type.