SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    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:

    Code:
    <SCRIPT LANGUAGE="javascript"><!--
    
    <?php print("var resloc = " . $goomapsloc . " \n");?>
    alert(resloc);
    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-"http://google.com. . 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:

    Code:
    src="http://www.gmodules.com/ig/ifr?url=http://hosting.gmodules.com/ig/gadgets/file/108836475058194723393/driving_directions.xml&amp;up_from=&amp;up_to=
    Code:
    twin%20peaks%20blvd%2C%20san%20francisco
    Code:
    &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"
    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?

    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2009
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    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 = "http://www.gmodules.com/ig/ifr?url=http://hosting.gmodules.com/ig/gadgets/file/108836475058194723393/driving_directions.xml&amp;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

Bookmarks

Posting Permissions

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