简体   繁体   中英

Set sessionStorage in JSONP results?

I'm trying to set sessionStorage using HTML5 in the result handler of an AJAX JSONP request. To explain this, lets say my AJAX is like this:

$(document).ready(function(){
    var output = $('.dotted-list');

    $.ajax({
        url: 'page.php?sub='+categories+'',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
            $.each(data, function(i,item){ 
                var landmark = '<a href="products.html"  data-transition="slidefade" onClick="sessionStorage.someKey = 'Something';">'
                + '<div class="sub-baners">'
                + '<img src='+item.aw_image_url+' >'
                + '</div>'
                + '</a>';
                output.append(landmark);
            });
        },
        error: function(){
            output.text('There was an error loading the data.');
        }
    });

As you can see I'm trying to set sessionStorage using an inline onClick="" but I know what I am doing is wrong because when I inspect the element in the browser instead of getting something like this:

 onClick="sessionStorage.someKey='Something';" 

I get a very strange output which is messed up! something like this:

Something;' onClick="sessionStorage.someKey = "

Could someone please advise on this issue?

It's a wrong code, but you can fix this:

  onClick="sessionStorage.someKey = \'Something\';"

Look at the escaped single quotes.

However, is better if you remove the onclick in the html and write a listener in a javascript file that allows you to control better and with a correct syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM