0

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!

2
  • Did you try declaring the variable globally instead of as a local variable? (Noting that ajax is asynchronous, so you couldn't actually use the value in the global variable until after the success callback runs.) Commented Aug 9, 2016 at 10:40
  • well, I tried something like that but wasn't really successfull. I don't really worked alot with JS.. Commented Aug 9, 2016 at 10:41

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I did that but it still doesn't work. If I console.log(tld) i'm getting the right output. If I use that in my code - like if(tld == .de) {... } nothing happen
Not entirely sure what you mean. Have you set a breakpoint on tld = data.tld;? I think it should be tld = data.data.tld;
I think tld = data.tld is enough cause I get the right output if I'm trying to console.log it.
1

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>

4 Comments

if I'm trying to use the variable in my if statement, just nothing happen
@WellNo check with your if statement when the data loaded
I've did a document.ready around the if statement but it still did nothing. The ajax code is at the bottom of my code. The if statemant at the top. Between them are about 300 lines of code, thats why I need to get the tld variable global.
the problem is not tdl here, before your ajax request success your global tdl variable is null
0

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">')
    }

4 Comments

yes, I have that like this in my code. I just wrote the if statement for an example quick in the question box
Have you tried console.log(tld) to check real value of this variable?
I did and in tld is the right value. But I can't use it everywhere in my code
In your if statement add console.log(tld) before each document.write(...). You should be able to see value of that variable. I think document.write() is messed up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.