V Posted October 8, 2010 Share Posted October 8, 2010 I'm creating an ad rotation script and trying to store this code.. amazon_ad_tag = "xxxxxxxxxxx-xxx"; amazon_ad_width = "160"; amazon_ad_height = "600"; as a global js variable. I'm not getting errors but values are missing when loading Amazon's script that displays the banner. This is how I inserted the code into the variable's value. I think that I did it wrong.. :-\ advert_options = "amazon_ad_tag = \"xxxxxxxxxxxx-xxx\"; amazon_ad_width = \"160\"; amazon_ad_height = \"600\";"; I use the "advert_options" variable to produce an Amazon banner, for example... <script type="text/javascript"> $(document).ready(function(){ advert_options = "amazon_ad_tag = \"xxxxxxxxxxxx-xxx\"; amazon_ad_width = \"160\"; amazon_ad_height = \"600\";"; $('.ad_slot').html('<scr'+'ipt type=\"text/javascript\"><!--' + advert_options + '//--></scr'+'ipt><scr'+'ipt src=\"' + window.location.protocol +'//www.assoc-amazon.com/s/ads.js\" type=\"text/javascr'+'ipt\"></scr'+'ipt>'); }); </script> <div class="ad_slot"></div> The output in the "ad_slot" div should be <script type="text/javascript"><!-- amazon_ad_tag = "xxxxxxxxxxxx-xx"; amazon_ad_width = "160"; amazon_ad_height = "600";//--></script> <script type="text/javascript" src="http://www.assoc-amazon.com/s/ads.js"></script> ...but I get amazon_ad_rcm is not defined Line 372 when Amazon's ads.js script loads. I have missing values I think. My question is, am I properly defining the "advert_options" var? Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/ Share on other sites More sharing options...
.josh Posted October 8, 2010 Share Posted October 8, 2010 Why are you even trying to write javascript to a div like that in the first place? Just do this: <script type="text/javascript"> $(document).ready(function(){ window['amazon_ad_tag'] = "xxxxxxxxxxxx-xxx"; window['amazon_ad_width'] = "160"; window['amazon_ad_height'] = "600"; }); </script> This will set your vars as global vars inside your jquery's document.ready function. Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120347 Share on other sites More sharing options...
V Posted October 8, 2010 Author Share Posted October 8, 2010 @Crayon thanks for the code but I'm not exactly sure how to use it. Adding the js to a div is the only way I can achieve what I need to. It's a bit long to explain but ultimately I'm trying to figure out how to put that entire code in a variable. Also, the vars are already global I believe because I didn't use "var" when defining them. Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120356 Share on other sites More sharing options...
.josh Posted October 8, 2010 Share Posted October 8, 2010 okay well honestly you should probably attempt to explain because I don't see why you need ot put it into a variable like that, but if you insist...look into eval() because ultimately your problem in the OP is that you are just outputting a single variable with the other variables as that variable's string value, so what you want to do is eval() that main variable. Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120359 Share on other sites More sharing options...
V Posted October 8, 2010 Author Share Posted October 8, 2010 Oooh eval() executes strings, it seems to be what I need! Thanks Crayon! Sorry for not explaining the whole thing, I didn't want to overwhelm. In short, it's for an ad rotation script that displays various ads (Amazon, ebay, adsense, etc). You can choose which type of ad you want to display (I do this in js with switch statements) and each ad has it's own affiliate id. So my script loads the codes that belong to a certain ad type the user chose to display. (I don't use the db for the entire code, just for unique affiliate links) The reason I want to put the whole code in 1 variable string is because I want to maintain the original ad code format so I don't violate any policies. Adsense for instance prohibits alteration of their code. lol that wasn't very short, not sure if it makes sense. I'm certain there must various way to achieve that, maybe I'll figure out a smarter way once the script starts working. Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120391 Share on other sites More sharing options...
V Posted October 8, 2010 Author Share Posted October 8, 2010 Oooh eval() executes strings, it seems to be what I need! Thanks Crayon! Sorry for not explaining the whole thing, I didn't want to overwhelm. In short, it's for an ad rotation script that displays various ads (Amazon, ebay, adsense, etc). You can choose which type of ad you want to display (I do this in js with switch statements) and each ad has it's own affiliate id. So my script loads the codes that belong to a certain ad type the user chose to display. (I don't use the db for the entire code, just for unique affiliate links) The reason I want to put the whole code in 1 variable string is because I want to maintain the original ad code format so I don't violate any policies. Adsense for instance prohibits alteration of their code. lol that wasn't very short, not sure if it makes sense. I'm certain there must various way to achieve that, maybe I'll figure out a smarter way once the script starts working. Sorry for the double post.. it seems there's no way to delete it. Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120395 Share on other sites More sharing options...
.josh Posted October 8, 2010 Share Posted October 8, 2010 Sounds like maybe instead you could have a wrapper function that you can pass an argument(s) to to specify which ad you want to use and the wrapper function would set the appropriate vars. But yeah I guess go for "get it working" first and go from there, but in general you should avoid using eval() Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120396 Share on other sites More sharing options...
V Posted October 8, 2010 Author Share Posted October 8, 2010 Sounds like maybe instead you could have a wrapper function that you can pass an argument(s) to to specify which ad you want to use and the wrapper function would set the appropriate vars. But yeah I guess go for "get it working" first and go from there, but in general you should avoid using eval() Hmm, I'll put the script together and I'll post it, it's difficult to explain. It's a very short script. Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120397 Share on other sites More sharing options...
V Posted October 8, 2010 Author Share Posted October 8, 2010 After reading about eval() disadvantages I decided not to use it. Anyways this is my script for the ad rotation. Most variable values will be defined dynamically by the user but for the sake of testing I already added values. I'm also using a selection list to test all ad types. <script type="text/javascript">$(function() {$('#affiliates').change(function() { //selected affiliate from list var affiliate_id = '12345678'; //adsense publisher id or other affiliate id switch (this.value) { case 'adsense': //ad styling and affiliate id ad_options = 'google_ad_client = "pub-' + affiliate_id + '"; google_ad_width = 160; google_ad_height = 600; google_ad_format = "160x600_as"; google_ad_type = "text_image"; google_color_border = "000000"; google_color_bg = "ffffff"; google_color_link = "0066CC"; google_color_text = "000000"; google_color_url = "3D81EE";'; //javascript tags and external ad script ad_js = '<scr'+'ipt type=\"text/javascript\"><!--' + ad_options + '//--></scr'+'ipt><scr'+'ipt src=\"' + window.location.protocol +'//pagead2.googlesyndication.com/pagead/show_ads.js\" type=\"text/javascr'+'ipt\"></scr'+'ipt>'; break; case 'amazon': //ad styling and affiliate id ad_options = 'amazon_ad_tag = \"xxxxxxx-xx\"; amazon_ad_width = \"160\"; amazon_ad_height = \"600\"'; //javascript tags and external ad script ad_js = '<scr'+'ipt type=\"text/javascript\"><!--' + ad_options + '//--></scr'+'ipt><scr'+'ipt src=\"' + window.location.protocol +'//www.assoc-amazon.com/s/ads.js\" type=\"text/javascr'+'ipt\"></scr'+'ipt>'; break; }//generate ad code in ad_slot div $('.ad_slot').html(ad_js); });});</script> then the HTML... <select id="affiliates"> <option value="0">Select Affiliate</option> <option value="adsense">adsense</option> <option value="amazon">amazon</option> <option value="other">etc</option></select><div class="ad_slot"></div> So if for example I select "Amazon" from the list, this code should appear in the "ad_slot" div. <script type="text/javascript"><!--amazon_ad_tag = "xxxxxxxxx-xx"; amazon_ad_width = "160"; amazon_ad_height = "600";//--></script><script type="text/javascript" src="http://www.assoc-amazon.com/s/ads.js"></script> I need to maintain the exact code format as generated on amazon.com or adsense. The issue is, values (like amazon_ad_width, amazon_ad_height, etc) aren't being sent in the external js. Quote Link to comment https://forums.phpfreaks.com/topic/215445-defining-a-js-variable/#findComment-1120406 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.