Help - passing variable inside of src

I am familiar with php, but not so much with JavaScript.

I found a Google Gadget that basically gets driving directions that I want to use on my web site. Basically there are two text boxes: one for starting point, and one for destination. I want the destination box to be filled in based on a php variable that controls the location.

In a nut shell: I need to use a php variable $locgoomaps and pass it to the javascript and use it within the SRC tag. But, being very new to javascript, I am finding this to be quite frustrating.

  1. Currently this is being placed in the body of my page.

What I have been able to accomplish so far is this:

<SCRIPT LANGUAGE="javascript"><!--

<?php print("var resloc = " . $goomapsloc . " \

Php will write the java code so that I can easily pass the variable. I have the alert to test that it is passed correctly (and it is working).

Now, I still can’t figure out how to get my javascript ‘resloc’ variable to work with my src tag.

I have tried to do an src-“ . blah. . blah. . loc=”+resloc+“&amp. . blah blah blah”

But this is not working. Nothing I have tried seems to work.

Here is the untouched src code that I need to pass my variable within:






The red text is what I need to be dynamic and replace with my variable, but I can’t seem to get this to come together . . where I pass the php variable to javascript and can use the new js variable in the src.

Any suggestions?


Say in your HTML your src attribute was that for an image tag (same principal applies to other HTML tags like an image tag) e.g.

<script src=“” id=“yourScriptID”></script>

In your script, you could then dynamically set the value of the script src attribute like this:

var resloc = “twin%20peaks%20blvd%2C%20san%20francisco”;

var srcValue = “;up_from=&amp;up_to=+ resloc + “&up_country=0&synd=open&w=400&h=140&title=Get+Directions&lang=en&country=CH&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js”

var yourScript = document.getElementById(‘yourScriptID’);
yourScript.src = resloc;