...

View Full Version : Variable is undefined, if else not working.



Toxhicide
12-12-2010, 09:29 PM
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.


<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'>

jmrker
12-12-2010, 11:10 PM
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.

karen mackay
12-12-2010, 11:13 PM
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

Toxhicide
12-12-2010, 11:24 PM
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.


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.

jmrker
12-12-2010, 11:49 PM
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??? :confused:
What does that mean?
Too many if's for me to decipher. :eek:

Toxhicide
12-13-2010, 12:01 AM
Huh??? :confused:
What does that mean?
Too many if's for me to decipher. :eek:

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.

Old Pedant
12-13-2010, 01:27 AM
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".

Toxhicide
12-13-2010, 02:33 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum