Tuesday, January 31, 2012

Variables in javascript?

I am having some problems using a variable within a javascript src.



I have little javascript experience, but a lot of php. Here is what I am trying to do:



1) I am using google maps to get directions for users on a web application. But, the destination is not static. It changes. I am storing the destination in a php variable called $goomapsloc.



Now, in order to make the php variable available within the src, I have done the following:



(I have added some spaces, to prevent yahoo from truncating the code . . as recommended by another user . . so please ignore any spacing issues)



%26lt;script type="text/javascript"%26gt;



%26lt;?php print("var resloc = " . $goomapsloc . " \n");?%26gt;



src="http://www.gmodules.com/ig/ifr? url=http://hosting.gmodules.com/ig/gadge鈥?114281111391296844949/driving- directions.xml%26amp;amp ;up_fromLocation=%26amp;up_myLocations=" + resloc + ";up_defaultDirectionsType= %26amp;synd=open%26amp;w=400%26amp;h= 75%26amp;title =Directions+by+Google+Maps%26amp; brand=light%26amp;lang= en%26amp;country=US%26amp;borde r=%23ffffff%7C3px%2C1px+solid+ %23999999%26amp;output=js"%26gt; %26lt;/script%26gt;





That is what I have. The php part is working just fine, but the src part fails. All I get for resloc is the exact text of "resloc" not the variable . . .



Any assistance to make this work ?Variables in javascript?
If that is your actual javascript code, you are prematurely closing of your javascript reference (in the first line: "%26lt;script type="text/javascript"%26gt;")



I assume what you want in the end is something similar to

%26lt;script type="text/javascript" src="http://www.gmodules.com/ig/ifr? url=http://hosting.gmodules.com/ig/gadge鈥?114281111391296844949/driving- directions.xml%26amp;amp ;up_fromLocation=%26amp;up_myLocations=" + resloc + ";up_defaultDirectionsType= %26amp;synd=open%26amp;w=400%26amp;h= 75%26amp;title =Directions+by+Google+Maps%26amp; brand=light%26amp;lang= en%26amp;country=US%26amp;borde r=%23ffffff%7C3px%2C1px+solid+ %23999999%26amp;output=js"%26gt;

%26lt;/script%26gt;



If that's the case, simply get rid of that "%26gt;"

No comments:

Post a Comment