Global Variable within a function.. I've tried all methods on forum no luck | Newbie | | Join Date: Oct 2009
Posts: 1
| |
I would like to store a variable then call it back later.
I have a variable on line 198 - www = ''+this._ad.clickUrl+'';
and on line 321 i try
I've tried so many different options, just won't work. any ideas??? i have even tried to call document.write(window.www);
Thanks
The code is below.. - <html>
-
<head>
-
<title>Integrated Ad Sample</title>
-
-
<!-- NOTE MANDATORY jQuery Include -->
-
<script type="text/javascript" src="http://icons.cubics.com/script/jquery-1.3.2.min.js"></script>
-
-
<!-- Integrated Ad Include-->
-
<script type="text/javascript">
-
-
-
-
if(typeof(Adknowledge) == "undefined" || !Adknowledge)
-
{
-
Adknowledge = {};
-
}
-
-
-
Adknowledge.AdFeed = function()
-
{
-
this._url = "http://social.bidsystem.com/integratedAd.aspx?";
-
this._numAds = 0;
-
this._adsJson = null;
-
this._timeout = 30000;
-
this._channel = '';
-
this._callback = null;
-
this._callbackArg = null;
-
}
-
-
Adknowledge.AdFeed.prototype =
-
{
-
get_numAds: function()
-
{
-
return this._numAds;
-
},
-
-
get_ads: function()
-
{
-
return this._adsJson;
-
},
-
-
retrieve : function(n, publisherId, applicationId, platformId, channel, callback, callbackArg)
-
{
-
var self = this;
-
this._channel = channel;
-
this._callback = callback;
-
this._callbackArg = callbackArg;
-
-
// Request ads from feed and save in this object
-
jQuery.ajax({
-
type: "GET",
-
url: this._url,
-
data: { 'adsRequested': n, 'channel': this._channel, 'pid': publisherId, 'appId': applicationId, 'plid': platformId, 'adSize': 'integrated' },
-
dataType: "jsonp",
-
success: function(data) { self._onFeedLoaded.call(self, data); },
-
error: function(data) { self._onFeedError.call(self, data, responseStatus); },
-
complete: function() { self._onComplete.call(self); },
-
timeout: this._timeout
-
});
-
},
-
-
_onFeedLoaded : function(data, responseStatus)
-
{
-
this._adsJson = data;
-
this._numAds = data.length;
-
},
-
-
_onFeedError : function(request, status, error)
-
{
-
this._adsJson = [];
-
this._numAds = 0;
-
},
-
-
_onComplete : function()
-
{
-
if(this._callback != null)
-
{
-
this._callback(this, this._callbackArg);
-
}
-
}
-
};
-
-
Adknowledge.IntegratedAd = function(idSuffix)
-
{
-
this._idSuffix = idSuffix;
-
this._ad = null;
-
this._autoLink = true;
-
-
this._js = null;
-
}
-
Adknowledge.IntegratedAd.prototype =
-
{
-
addRegex: function()
-
{
-
jQuery.expr[':'].regex = function(elem, index, match)
-
{
-
var matchParams = match[3].split(','),
-
validLabels = /^(data|css):/,
-
attr =
-
{
-
method: matchParams[0].match(validLabels) ? matchParams[0].split(':')[0] : 'attr',
-
property: matchParams.shift().replace(validLabels,'')
-
},
-
regexFlags = 'ig',
-
regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
-
return regex.test(jQuery(elem)[attr.method](attr.property));
-
}
-
},
-
-
include_js_special: function(file, publisherId, applicationId, platformId)
-
{
-
var self = this;
-
var html_doc = document.getElementsByTagName('head')[0];
-
this._js = document.createElement('script');
-
this._js.setAttribute('type', 'text/javascript');
-
this._js.setAttribute('src', file);
-
html_doc.appendChild(this._js);
-
-
this._js.onreadystatechange = function () {
-
Adknowledge.IntegratedAd.RenderAll(publisherId, applicationId, platformId);
-
}
-
-
this._js.onload = function () {
-
Adknowledge.IntegratedAd.RenderAll(publisherId, applicationId, platformId);
-
}
-
return false;
-
},
-
-
get_ad: function()
-
{
-
return this._ad;
-
},
-
set_ad: function(value)
-
{
-
this._ad = value;
-
},
-
-
get_autoLink: function()
-
{
-
return this._autoLink;
-
},
-
set_autoLink: function(value)
-
{
-
this._autoLink = value;
-
},
-
-
render: function()
-
{
-
var title = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_TITLE+this._idSuffix+')');
-
var description = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_DESCRIPTION+this._idSuffix+')');
-
var product = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_PRODUCT+this._idSuffix+')');
-
var callToAction = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CALL+this._idSuffix+')');
-
var click = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CLICK+this._idSuffix+')');
-
var icon = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_ICON+this._idSuffix+')');
-
var pixel = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_PIXEL+this._idSuffix+')');
-
var currencyAmount = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CURRENCY_AMOUNT+this._idSuffix+')');
-
var currencyName = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CURRENCY_NAME+this._idSuffix+')');
-
-
if(this._ad != null)
-
{
-
title.empty().append(this._ad.title);
-
description.empty().append(this._ad.description);
-
product.empty().append(this._ad.productName);
-
callToAction.empty().append(this._ad.callToAction);
-
click.empty().append(this._ad.clickUrl);
-
currencyAmount.empty().append(this._ad.currencyAmount);
-
currencyName.empty().append(this._ad.currencyName);
-
icon.attr('src', this._ad.iconUrl);
-
-
// Generate channel name based on page and ad suffix
-
if(pixel.size() <= 0)
-
{
-
// Pixel element not found, create one to ensure the impression registers
-
jQuery(document.body).append('<img src="'+this._ad.pixelUrl+'" height="1" width="1" />');
-
}
-
else
-
{
-
// Pixel element exists, set the image src
-
pixel.attr('src', this._ad.pixelUrl);
-
}
-
-
-
-
// CODING
-
-
// If auto-linking display elements, add the links now
-
if(this._autoLink === true)
-
{
-
var clickScript = '<a href="'+this._ad.clickUrl+'\';"></a>';
-
-
title.wrapInner(clickScript);
-
description.wrapInner(clickScript);
-
product.wrapInner(clickScript);
-
callToAction.wrapInner(clickScript);
-
icon.wrap(clickScript);
-
-
-
// I WOULD LIKE TO TAKE THIS VARIABLE AND DISPLAY IT ON LINE 319 BUT IT JUST WONT DISPLAY
-
www = ''+this._ad.clickUrl+'';
-
// IF I TRY DISPLAY IT HERE IT WORKS
-
// document.write(www);
-
}
-
-
-
-
}
-
else
-
{
-
-
title.hide();
-
description.hide();
-
product.hide();
-
callToAction.hide();
-
click.hide();
-
currencyAmount.hide();
-
currencyName.hide();
-
icon.hide();
-
}
-
-
-
-
-
-
}
-
-
-
};
-
-
-
-
Adknowledge.IntegratedAd.DEBUG = false;
-
Adknowledge.IntegratedAd.ID_PREFIX = '_adk-';
-
Adknowledge.IntegratedAd.ID_TITLE = 'title-';
-
Adknowledge.IntegratedAd.ID_DESCRIPTION = 'description-';
-
Adknowledge.IntegratedAd.ID_PRODUCT = 'product-';
-
Adknowledge.IntegratedAd.ID_CALL = 'callToAction-';
-
Adknowledge.IntegratedAd.ID_CLICK = 'click-';
-
Adknowledge.IntegratedAd.ID_ICON = 'icon-';
-
Adknowledge.IntegratedAd.ID_PIXEL = 'pixel-';
-
Adknowledge.IntegratedAd.ID_CURRENCY_AMOUNT = 'currencyAmount-';
-
Adknowledge.IntegratedAd.ID_CURRENCY_NAME = 'currencyName-';
-
Adknowledge.IntegratedAd.RenderAll = function(publisherId, applicationId, platformId)
-
-
-
-
{
-
if(typeof(jQuery) == "undefined" || !jQuery)
-
{
-
ia = new Adknowledge.IntegratedAd();
-
ia.include_js_special('http://icons.cubics.com/script/jquery-1.3.2.min.js', publisherId, applicationId, platformId);
-
} else {
-
ia = new Adknowledge.IntegratedAd();
-
ia.addRegex();
-
var adNames = [];
-
var ads = [];
-
var regex = /^\_adk-(\w*)-(.*)$/ig;
-
-
// Find all elements matching the search pattern
-
var elements = jQuery(':regex(id,^'+Adknowledge.IntegratedAd.ID_PREFIX+'('+Adknowledge.IntegratedAd.ID_TITLE+'|'+Adknowledge.IntegratedAd.ID_PRODUCT+').*$)');
-
elements.each(function(i, e)
-
{
-
regex.lastIndex = 0;
-
// Parse unique ad identifiers
-
var idMatch = regex.exec(e.id);
-
if(idMatch && idMatch.length >= 3)
-
{
-
var exists = false;
-
for(var a = 0; a < adNames.length && !exists; a++)
-
{
-
if(adNames[a] == idMatch[2])
-
{
-
exists = true;
-
}
-
}
-
-
if(!exists)
-
{
-
adNames[adNames.length] = idMatch[2];
-
}
-
}
-
idMatch = null;
-
});
-
-
// Create ad widget
-
for(var a = 0; a < adNames.length; a++)
-
{
-
var feed = new Adknowledge.AdFeed();
-
var suffix = adNames[a];
-
ads[a] = new Adknowledge.IntegratedAd(adNames[a]);
-
-
// Generate channel name
-
var channel = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1)+' - '+adNames[a];
-
-
var callback = function(f, i)
-
{
-
if(f.get_numAds() > 0)
-
{
-
ads[i].set_ad(f.get_ads()[0]);
-
}
-
ads[i].render();
-
};
-
-
// Attempt to retrieve an ad
-
feed.retrieve(1, publisherId, applicationId, platformId, channel, callback, a);
-
}
-
}
-
};
-
-
-
-
-
</script>
-
-
<script type="text/javascript">
-
Adknowledge.IntegratedAd.RenderAll(1233, 1234, 12222); //publisherId, appId, platformId
-
</script>
-
-
<script type="text/javascript">
-
-
// I WOULD LIKE TO DISPLAY THE VARIABLE HERE
-
document.write(www);
-
-
</script>
-
-
</body>
-
</html>
-
-
-
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,648
| | | re: Global Variable within a function.. I've tried all methods on forum no luck
I don’t know if that’s the problem, but you’re missing </head> & <body> tags…
further document.write() is not the method of choice to add HTML to the document. (preferred methods would be appendChild() or innerHTML)
further, if you have a “static” object (not instantiated with new) I think you can’t use this to reference the objects properties and if you indeed use a “static” object the use of prototype brings no advantage.
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|