Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to embed JS variable in img src attribute?

    Hi,

    I have an external javascript file with the following code:
    loct = document.location
    str = new String(loct);
    lind = str.lastIndexOf("/" );
    burl = str.substring(0,lind);
    iurl = burl + "/imagefolder/"
    alert("the image url is " + iurl);

    What this does:
    It gets the local html file location (eg. file:///x:/path/tomy/htmlfile.html), removes the file name and appends "/imagefolder/" string to it to give the image files folder location (eg. file:///x:/path/tomy/imagefolder/). Alert() is for debugging purposes.

    In the html file, I have put the script src
    Now, I want to embed javascript in the src attribute of the Image like:
    <img src="&{iurl};ImageFile.gif" border=0>

    On opening the html file, the alert comes out properly, but the image is not shown, eventhough the "ImageFile.gif" is in its mentioned location. I have tried both in firefox 1.5 and IE 6. Where have I gone wrong?

    Thanks for any directions.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    To create/change elements/attributes you need methods. Methods are usually nested in functions, functions are triggerend by events. This is the way javscript works. It is a dynamic language, if you know already another language, a server-side one, you should not use the same static pattern in thinking javascript code.

    <img onload="this.src=iurl+'mageFile.gif'">

    make sure your iurl variable is a global one.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply, but I was trying this method after reading the following link:
    "Using JavaScript expressions as HTML attribute values": http://wp.netscape.com/eng/mozilla/3...rt.htm#1016301

    Is it possible to make it work as shown in the above link?

    Meanwhile I will try your method also, but for my purposes, the method given in the link is highly suited. I wish I could make it work.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    It should work, based on your piece of code. My question is which is the event to change the src attribute? In other words, which is the action of the user which should do that change?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New to the CF scene
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    According to the example given in the page I mentioned, there need not be any trigger to run the code. I also tried your method, it also didn't work. I checked again - javascript is enabled in my browser (getting the alert dialog I have inserted), the image is in the mentioned location (I've put the absolute path directly without javascript and it is shown). What more should I do? Thanks for any advice you might give.


  •  

    Posting Permissions

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