Connecting Tech Pros Worldwide Forums | Help | Site Map

Variables not declared and "undefined" property values

David R
Guest
 
Posts: n/a
#1: Nov 7 '05
'lo

This one is confusing me, causing 2 separate errors:

Consider this XHTML and javascript:

<!-- ########################## -->
<!-- SNIP -->
<head>
<script type="text/javascript"
src="http://login.gamespyid.com/ads/jscheck.aspx"></script>
<script type="text/javascript"
src="http://wrapper.gamespyid.com/a?pagetype=ccontent&amp;size=468x60"></script>
<script type="text/javascript" src="GSScripts.js"></script>
</head>
<html>
<!-- SNIP -->
<ul id="ex_Subscribe">
<li><a href="Foo">Buy Games</a></li>
</ul>
<!-- SNIP -->
</html>
<!-- ########################## -->
The 1st Gamespy javascript *requires* a HTTP Referer (sic) field to be
present, fake any if you wish.

GSScripts.js contains this:

/////////////////////////

var legalsite = "NewPlanetSite";
var advertise = "/site/contact.shtml#ads";
var legal = "/site/legal.shtml";
var strAdsEnabled = "";
var strUpgradeLink = "";
window.onload = function() {
if ( adsAreDisabled ) {
strAdsEnabled = '<li><a
href="https://login.gamespyid.com/ads/ads.aspx?off=1">Enable Ads</a></li>';
} else {
strAdsEnabled = '<li><a
href="https://login.gamespyid.com/ads/ads.aspx">Disable Ads</a></li>';
}
if ( isLoggedIn ) {
strUpgradeLink = '<li><a
href="https://login.gamespy.com/Subscribe/upgrade.aspx">Upgrade
Now!</a></li>';
} else {
strUpgradeLink = '<li><a
href="http://net.gamespyid.com/linktrack/redirect.aspx?i=9565&t=http%3a%2f%2fsubscribe.igng amespy.com%2ffcup%2fhitpoints.html">Subscribe
Now!</a></li>';
}
var ulEx = document.getElementById("ex_Subscribe");
ulEx.InnerHtml = ulEx.InnerHtml + strAdsEnabled + strUpgradeLink;
}

/////////////////////////

And guess what?

It doesn't work! XD

The 2 problems are:

a) The "adsAreDisabled" variable declared and assigned in jscheck.aspx are
not carried over to GSScripts.js, so it throws an NRE error for the "if
(adsAreDisabled) {" line. Is there any workaround for this? (To see problem
#2 just comment out all the "if" and brace lines)

b) Setting the .InnerHtml property doesn't do anything; I did an "alert (
strAdsEnabled );" and that worked. But when I did "alert (
ulEx.InnerHtml );" I got "undefined". However, "alert ( ulEx );" works fine.

Any ideas?

Thanks



RobG
Guest
 
Posts: n/a
#2: Nov 7 '05

re: Variables not declared and "undefined" property values


David R wrote:[color=blue]
> 'lo
>
> This one is confusing me, causing 2 separate errors:
>[/color]
[...]
[color=blue]
> And guess what?
>
> It doesn't work! XD
>
> The 2 problems are:
>
> a) The "adsAreDisabled" variable declared and assigned in jscheck.aspx are
> not carried over to GSScripts.js, so it throws an NRE error for the "if
> (adsAreDisabled) {" line. Is there any workaround for this? (To see problem
> #2 just comment out all the "if" and brace lines)[/color]

I can't test this, but it could be that the jscheck.aspx script has not
loaded when your anonymous function is executed (the right hand side
will be evaluated & executed immediately it is parsed, not when the page
has finished loading).
[color=blue]
>
> b) Setting the .InnerHtml property doesn't do anything; I did an "alert (
> strAdsEnabled );" and that worked. But when I did "alert (
> ulEx.InnerHtml );" I got "undefined". However, "alert ( ulEx );" works fine.[/color]

JavaScript is case sensitive: the property you seek is elEx.innerHTML
(lower case 'i').



--
Rob
Closed Thread


Similar JavaScript / Ajax / DHTML bytes