简体   繁体   中英

set javascript variable from an ajax request globally?

I have this ajax code right here:

<script>
    $('a[data-id]').click(function () {
        var id = $(this).attr('data-id');
        var domain = $(this).attr('data-domain');
        $.ajax({
            url: 'getdata',
            type: 'GET',
            dataType: 'json',
            data: {id: id, domain: domain},
            success: function (data) {
                var domains = data.name + data.tld;
                var tld = data.tld;
                $('.resultdomain').html(domains);

            }
        });
    });
</script>

This code works but my problem is that I want to set the tld variable globally to use them in an if statement.

I want to use the variable like this in my code:

if(tld == .de)
{
document.write('<img src="imagelink.png" alt="denic" class="pull-right">')
}
elseif(tld == .com)
{
document.write('<img src="otherimagelink.png" alt="core" class="pull-right">')
}

but I couldn't figure out how I can set the tld variable globally to use it everywhere in my code.

Thanks for any help!

try defining tdl variable globally out of the scope

<script>
      var tdl;
      $('a[data-id]').click(function () {
        var id = $(this).attr('data-id');
        var domain = $(this).attr('data-domain');
        $.ajax({
            url: 'getdata',
            type: 'GET',
            dataType: 'json',
            data: {id: id, domain: domain},
            success: function (data) {
                var domains = data.name + data.tld;
                tld = data.tld;
                $('.resultdomain').html(domains);
                 if(tld == .de)
                 {
                    document.write('<img src="imagelink.png" alt="denic" class="pull-right">')
                 }
                 elseif(tld == .com)
                 {
                    document.write('<img src="otherimagelink.png" alt="core" class="pull-right">')
                 }
            }
        });
    });


</script>

The scope of tld variable is enclosed in your AJAX call. It is only available to use within the scope of its definition. I've moved it outside of your AJAX call.

   <script>
        var tld;
        $('a[data-id]').click(function () {
            var id = $(this).attr('data-id');
            var domain = $(this).attr('data-domain');
            $.ajax({
                url: 'getdata',
                type: 'GET',
                dataType: 'json',
                data: {id: id, domain: domain},
                success: function (data) {
                    var domains = data.name + data.tld;
                    tld = data.tld;
                    $('.resultdomain').html(domains);

                }
            });
        });
    </script>

You should check strings, not .de but ".de"

    if(tld == ".de")
    {
        document.write('<img src="imagelink.png" alt="denic" class="pull-right">')
    }
    elseif(tld == ".com")
    {
        document.write('<img src="otherimagelink.png" alt="core" class="pull-right">')
    }

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