SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    0 Post(s)
    0 Thread(s)

    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 . " \n");?>
    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?


  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2009
    0 Post(s)
    0 Thread(s)
    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 + "&amp;up_country=0&amp;synd=open&amp;w=400&amp;h=140&amp;title=Get+Directions&amp;lang=en&amp;country=CH&amp;border=%23ffffff%7C3px%2C1px+solid+%23999999&amp;output=js"

    var yourScript = document.getElementById('yourScriptID');
    yourScript.src = resloc;
    If at first you don't succeed... buy it from someone else
    Life's to short to be wasting time trying again and again!

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts