Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Variable is undefined, if else not working.

    When I click my button, window.frames['akFrame'].document.getElementsByName('auth_key').value is undefined, and I cant seem to make the if else statement work.

    Code:
    <script type='text/javascript'>
    url=location.href.split('/')[4];
    url=location.href.replace(url,'')
    
    if(url.charAt(url.length-1)!= '/'){
    url = url + '/'
    }
    
    Content = "<iframe width='0' height ='0' name='akFrame' id='akFrame' src='file:///C:/Users/Alec/Desktop/ffaa.html' style='display:none'></iframe>"
    
    function asdf(){
    authenKey = window.frames['akFrame'].document.getElementsByName('auth_key')[0].value
    if (authenKey)
    {
    document.getElementById('a').value = url + "&auth_key="+authenKey+"&good=true"
    }
    else
    {
    document.getElementById('a').value = url + "&auth_key=00&good=true"
    }
    }
    function apple(){
    document.getElementById('d').innerHTML = '<iframe src="" name="c" id="c"></iframe>'
    }
    document.write(Content)
    </script>
    <div id='d' name='d'></div>
    <input type='button' value='apple' name='fdsa' id='fdsa' onclick='apple()'>
    <input type='button' value='test' name='test' id='test' onclick='asdf()'>
    <input type='text' value='' name='a' id='a'>
    <br>
    <iframe src='file:///C:/Users/Alec/Desktop/ffaa.html' name='b' id='b'>
    Last edited by Toxhicide; 12-12-2010 at 10:33 PM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    Lightbulb

    I can't tell exactly from your code, but for sure there will be no value for 'auth_key'
    because it is created as a collection by the 'getElementsByName' function.

    There is not single value to check for in the IF statement.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm new here and I know this is not relative to your questions.

    but I can't figure out how to post a question. Can you help me.

    Thanks

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    I can't tell exactly from your code, but for sure there will be no value for 'auth_key'
    because it is created as a collection by the 'getElementsByName' function.

    There is not single value to check for in the IF statement.
    Yes, if you use this code on multiple websites, if there is an auth_key, for it to run a code, if there isnt for it to run another, which is for the if then. If authenKey is a variable, run, if authenKey is undefined, run else.

    Quote Originally Posted by karen mackay View Post
    I'm new here and I know this is not relative to your questions.

    but I can't figure out how to post a question. Can you help me.

    Thanks
    There is a new thread button in the forum.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    Question Huh? ...

    Quote Originally Posted by Toxhicide View Post
    Yes, if you use this code on multiple websites, if there is an auth_key, for it to run a code, if there isnt for it to run another, which is for the if then. If authenKey is a variable, run, if authenKey is undefined, run else.
    Huh???
    What does that mean?
    Too many if's for me to decipher.

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    Huh???
    What does that mean?
    Too many if's for me to decipher.
    authenKey = window.frames['akFrame'].document.getElementsByName('auth_key')[0].value

    So (not in code anymore),

    if authenKey is defined,
    code.

    if authenKey is undefined,
    other code.

    If window.frames['akFrame'].document.getElementsByName('auth_key')[0].value is undefined, then I get an error and the if else statement doesnt work.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,596
    Thanks
    80
    Thanked 4,631 Times in 4,594 Posts
    Code:
    var authenKey = "00"; // default if not found
    var temp = window.frames['akFrame'];
    if ( temp != null ) {
        temp = temp.document.getElementsByName('auth_key');
        if ( temp != null ) {
            temp = temp[0];
            if ( temp != null ) authenKey = temp.value;
       }
    }
    document.getElementById('a').value = 
         url + "&auth_key=" + authenKey + "&good=true";
    Hmmm??? Test all along the way for something missing. Only if everything is there do you change the value from the default of "00".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Toxhicide (12-13-2010)

  • #8
    New Coder
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    var authenKey = "00"; // default if not found
    var temp = window.frames['akFrame'];
    if ( temp != null ) {
        temp = temp.document.getElementsByName('auth_key');
        if ( temp != null ) {
            temp = temp[0];
            if ( temp != null ) authenKey = temp.value;
       }
    }
    document.getElementById('a').value = 
         url + "&auth_key=" + authenKey + "&good=true";
    Hmmm??? Test all along the way for something missing. Only if everything is there do you change the value from the default of "00".
    Thank you, this worked.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •