By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

Global Variable within a function.. I've tried all methods on forum no luck

P: 1
I would like to store a variable then call it back later.

I have a variable on line 198

Expand|Select|Wrap|Line Numbers
  1. www = ''+this._ad.clickUrl+'';  
and on line 321 i try

Expand|Select|Wrap|Line Numbers
  1. document.write(www); 
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..

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.     <title>Integrated Ad Sample</title>
  4.  
  5.     <!-- NOTE MANDATORY jQuery Include -->
  6.     <script type="text/javascript" src="http://icons.cubics.com/script/jquery-1.3.2.min.js"></script>
  7.  
  8.     <!-- Integrated Ad Include-->
  9.      <script type="text/javascript"> 
  10.  
  11.  
  12.  
  13.       if(typeof(Adknowledge) == "undefined" || !Adknowledge)
  14. {
  15.     Adknowledge = {};
  16. }
  17.  
  18.  
  19. Adknowledge.AdFeed = function()
  20. {
  21.     this._url = "http://social.bidsystem.com/integratedAd.aspx?";
  22.     this._numAds = 0;
  23.     this._adsJson = null;
  24.     this._timeout = 30000;
  25.     this._channel = '';
  26.     this._callback = null;
  27.     this._callbackArg = null;
  28. }
  29.  
  30. Adknowledge.AdFeed.prototype =
  31. {
  32.     get_numAds: function()
  33.     {
  34.         return this._numAds;
  35.     },
  36.  
  37.     get_ads: function()
  38.     {
  39.         return this._adsJson;
  40.     },
  41.  
  42.     retrieve : function(n, publisherId, applicationId, platformId, channel, callback, callbackArg) 
  43.     {
  44.         var self = this;
  45.         this._channel = channel;
  46.         this._callback = callback;
  47.         this._callbackArg = callbackArg;
  48.  
  49.         // Request ads from feed and save in this object
  50.         jQuery.ajax({
  51.             type: "GET",
  52.             url: this._url,
  53.             data: { 'adsRequested': n, 'channel': this._channel, 'pid': publisherId, 'appId': applicationId, 'plid': platformId, 'adSize': 'integrated' },
  54.             dataType: "jsonp",
  55.             success: function(data) { self._onFeedLoaded.call(self, data); },
  56.             error: function(data) { self._onFeedError.call(self, data, responseStatus); },
  57.             complete: function() { self._onComplete.call(self); },
  58.             timeout: this._timeout
  59.         });
  60.     },
  61.  
  62.     _onFeedLoaded : function(data, responseStatus)
  63.     {
  64.         this._adsJson = data;
  65.         this._numAds = data.length;
  66.     },
  67.  
  68.     _onFeedError : function(request, status, error)
  69.     {
  70.         this._adsJson = [];
  71.         this._numAds = 0;
  72.     },
  73.  
  74.     _onComplete : function()
  75.     {
  76.         if(this._callback != null)
  77.         {
  78.             this._callback(this, this._callbackArg);
  79.         }
  80.     }
  81. };
  82.  
  83. Adknowledge.IntegratedAd = function(idSuffix)
  84. {
  85.     this._idSuffix = idSuffix;
  86.     this._ad = null;
  87.     this._autoLink = true;
  88.  
  89.     this._js = null;
  90. }
  91. Adknowledge.IntegratedAd.prototype = 
  92. {
  93.     addRegex: function()
  94.     {
  95.         jQuery.expr[':'].regex = function(elem, index, match) 
  96.         {
  97.             var matchParams = match[3].split(','),
  98.                 validLabels = /^(data|css):/,
  99.                 attr = 
  100.                 {
  101.                     method: matchParams[0].match(validLabels) ? matchParams[0].split(':')[0] : 'attr',
  102.                     property: matchParams.shift().replace(validLabels,'')
  103.                 },
  104.                 regexFlags = 'ig',
  105.                 regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
  106.             return regex.test(jQuery(elem)[attr.method](attr.property));
  107.         }
  108.     },
  109.  
  110.     include_js_special: function(file, publisherId, applicationId, platformId)
  111.     {
  112.         var self = this;
  113.         var html_doc = document.getElementsByTagName('head')[0];
  114.         this._js = document.createElement('script');
  115.         this._js.setAttribute('type', 'text/javascript');
  116.         this._js.setAttribute('src', file);
  117.         html_doc.appendChild(this._js);
  118.  
  119.         this._js.onreadystatechange = function () {
  120.             Adknowledge.IntegratedAd.RenderAll(publisherId, applicationId, platformId);
  121.         }
  122.  
  123.         this._js.onload = function () {
  124.             Adknowledge.IntegratedAd.RenderAll(publisherId, applicationId, platformId);
  125.         }
  126.         return false;
  127.     },
  128.  
  129.     get_ad: function()
  130.     {
  131.         return this._ad;
  132.     },
  133.     set_ad: function(value)
  134.     {
  135.         this._ad = value;
  136.     },
  137.  
  138.     get_autoLink: function()
  139.     {
  140.         return this._autoLink;
  141.     },
  142.     set_autoLink: function(value)
  143.     {
  144.         this._autoLink = value;
  145.     },
  146.  
  147.     render: function()
  148.     {
  149.         var title = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_TITLE+this._idSuffix+')');
  150.         var description = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_DESCRIPTION+this._idSuffix+')');
  151.         var product = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_PRODUCT+this._idSuffix+')');
  152.         var callToAction = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CALL+this._idSuffix+')');
  153.         var click = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CLICK+this._idSuffix+')');
  154.         var icon = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_ICON+this._idSuffix+')');
  155.         var pixel = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_PIXEL+this._idSuffix+')');
  156.         var currencyAmount = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CURRENCY_AMOUNT+this._idSuffix+')');
  157.         var currencyName = jQuery(':regex(id,'+Adknowledge.IntegratedAd.ID_PREFIX+Adknowledge.IntegratedAd.ID_CURRENCY_NAME+this._idSuffix+')');
  158.  
  159.         if(this._ad != null)
  160.         {
  161.             title.empty().append(this._ad.title);
  162.             description.empty().append(this._ad.description);
  163.             product.empty().append(this._ad.productName);
  164.             callToAction.empty().append(this._ad.callToAction);
  165.             click.empty().append(this._ad.clickUrl);
  166.             currencyAmount.empty().append(this._ad.currencyAmount);
  167.             currencyName.empty().append(this._ad.currencyName);
  168.             icon.attr('src', this._ad.iconUrl);
  169.  
  170.             // Generate channel name based on page and ad suffix
  171.             if(pixel.size() <= 0)
  172.             {
  173.                 // Pixel element not found, create one to ensure the impression registers
  174.                 jQuery(document.body).append('<img src="'+this._ad.pixelUrl+'" height="1" width="1" />');
  175.             }
  176.             else
  177.             {
  178.                 // Pixel element exists, set the image src
  179.                 pixel.attr('src', this._ad.pixelUrl);
  180.             }
  181.  
  182.  
  183.  
  184.    // CODING      
  185.  
  186.               // If auto-linking display elements, add the links now
  187.             if(this._autoLink === true)
  188.             {
  189.                 var clickScript = '<a href="'+this._ad.clickUrl+'\';"></a>';
  190.  
  191.                 title.wrapInner(clickScript);
  192.                 description.wrapInner(clickScript);
  193.                 product.wrapInner(clickScript);
  194.                 callToAction.wrapInner(clickScript);
  195.                 icon.wrap(clickScript);
  196.  
  197.  
  198.                 // I WOULD LIKE TO TAKE THIS VARIABLE AND DISPLAY IT ON LINE  319 BUT IT JUST WONT DISPLAY
  199.                 www = ''+this._ad.clickUrl+'';
  200.                 // IF I TRY DISPLAY IT HERE IT WORKS
  201.                 // document.write(www);
  202. }
  203.  
  204.  
  205.  
  206.         }
  207.     else
  208.         {
  209.  
  210.             title.hide();
  211.             description.hide();
  212.             product.hide();
  213.             callToAction.hide();
  214.             click.hide();
  215.             currencyAmount.hide();
  216.             currencyName.hide();
  217.             icon.hide();  
  218.         }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.     }
  225.  
  226.  
  227. };
  228.  
  229.  
  230.  
  231. Adknowledge.IntegratedAd.DEBUG = false;
  232. Adknowledge.IntegratedAd.ID_PREFIX = '_adk-';
  233. Adknowledge.IntegratedAd.ID_TITLE = 'title-';
  234. Adknowledge.IntegratedAd.ID_DESCRIPTION = 'description-';
  235. Adknowledge.IntegratedAd.ID_PRODUCT = 'product-';
  236. Adknowledge.IntegratedAd.ID_CALL = 'callToAction-';
  237. Adknowledge.IntegratedAd.ID_CLICK = 'click-';
  238. Adknowledge.IntegratedAd.ID_ICON = 'icon-';
  239. Adknowledge.IntegratedAd.ID_PIXEL = 'pixel-';
  240. Adknowledge.IntegratedAd.ID_CURRENCY_AMOUNT = 'currencyAmount-';
  241. Adknowledge.IntegratedAd.ID_CURRENCY_NAME = 'currencyName-';
  242. Adknowledge.IntegratedAd.RenderAll = function(publisherId, applicationId, platformId)
  243.  
  244.  
  245.  
  246. {
  247.     if(typeof(jQuery) == "undefined" || !jQuery)
  248.     {
  249.         ia = new Adknowledge.IntegratedAd();
  250.         ia.include_js_special('http://icons.cubics.com/script/jquery-1.3.2.min.js', publisherId, applicationId, platformId);
  251.     } else {
  252.         ia = new Adknowledge.IntegratedAd();
  253.         ia.addRegex();
  254.         var adNames = [];
  255.         var ads = [];
  256.         var regex = /^\_adk-(\w*)-(.*)$/ig;
  257.  
  258.         // Find all elements matching the search pattern
  259.         var elements = jQuery(':regex(id,^'+Adknowledge.IntegratedAd.ID_PREFIX+'('+Adknowledge.IntegratedAd.ID_TITLE+'|'+Adknowledge.IntegratedAd.ID_PRODUCT+').*$)');
  260.         elements.each(function(i, e)
  261.         {
  262.             regex.lastIndex = 0;
  263.             // Parse unique ad identifiers
  264.             var idMatch = regex.exec(e.id);
  265.             if(idMatch && idMatch.length >= 3)
  266.             {
  267.                 var exists = false;
  268.                 for(var a = 0; a < adNames.length && !exists; a++)
  269.                 {
  270.                     if(adNames[a] == idMatch[2])
  271.                     {
  272.                         exists = true;
  273.                     }
  274.                 }
  275.  
  276.                 if(!exists)
  277.                 {
  278.                     adNames[adNames.length] = idMatch[2];
  279.                 }
  280.             }
  281.             idMatch = null;
  282.         });
  283.  
  284.         // Create ad widget
  285.         for(var a = 0; a < adNames.length; a++)
  286.         {
  287.             var feed = new Adknowledge.AdFeed();
  288.             var suffix = adNames[a];
  289.             ads[a] = new Adknowledge.IntegratedAd(adNames[a]);
  290.  
  291.             // Generate channel name
  292.             var channel = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1)+' - '+adNames[a];
  293.  
  294.             var callback = function(f, i)
  295.             {
  296.                 if(f.get_numAds() > 0)
  297.                 {
  298.                     ads[i].set_ad(f.get_ads()[0]);
  299.                 }
  300.                 ads[i].render();
  301.             };
  302.  
  303.             // Attempt to retrieve an ad
  304.             feed.retrieve(1, publisherId, applicationId, platformId, channel, callback, a);
  305.         }
  306.     }
  307. };
  308.  
  309.  
  310.  
  311.  
  312.      </script>
  313.  
  314. <script type="text/javascript">
  315. Adknowledge.IntegratedAd.RenderAll(1233, 1234, 12222);  //publisherId, appId, platformId  
  316. </script>
  317.  
  318. <script type="text/javascript">
  319.  
  320.    // I WOULD LIKE TO DISPLAY THE VARIABLE HERE
  321.    document.write(www);
  322.  
  323. </script>
  324.  
  325. </body>
  326. </html>
  327.  
  328.  
  329.  
Oct 29 '09 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
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.
Oct 29 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.