Jump to content

Defining a js variable


V

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.