How to decode javascript variable value which contains html element

I have one variable with text and an 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 is passed to ajax and this message is displayed 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 works fine, but in the popup HTML I get the anchor tag like this:

<a title="\&quot;Login\&quot;" class="\&quot;login\&quot;" id="\&quot;popup-login\&quot;" href="\&quot;javascript:void(0);\&quot;">login</a>`

      

One of the weird things is that this code works fine 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 should you do to get the correct anchor element?

+3


source to share


2 answers


Try encodeURI

the javascript function:

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);

      

Read more in the documentation .



OR

You can pass the flag message in the ajax request and based on that you can generate this 'Please <a href="javascript:void(0);" id="popup-login" class="login" title="Login">login</a>+ to save your wishlist.';

server side HTML. This will be good practice.

0


source


As per jquery ajax

: when posting data to the server, it will be converted to a query string, so the quotes will be escaped.



The data parameter 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 to a query string using jQuery.param () before submitting. This processing can be circumvented by setting the processData parameter to false. may not be desirable if you want to send an XML object to the server; in this case, change the contentType parameter from application / x-www-form-urlencoded to a more appropriate MIME type.

0


source







All Articles