Link to home
Start Free TrialLog in
Avatar of Techsavy
Techsavy

asked on

set src attribute to a javascript function

Hi,

What I am trying to do is build a dynamic url for my src attribute in img element. such as:

 <img id="visionimage" src="javascript:getImageUrl('visionimage','vision.jpg')" alt="vision" /><span>Vision</span>
   
where:
 function getImageUrl(imagetitle)
  {
 
    var siteUrl = getSiteUrl();
    var imageUrl = siteUrl + "/SiteAssets/images/" + imagetitle;
    return imageUrl;
   
  }
 

but this is not working. How to achieve this?

Please help!
Avatar of Gary
Gary
Flag of Ireland image

Not going to happen - blocked by the browser
Why don't you just run the js when the page is loaded. Storing the image name in data attribute.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Techsavy
Techsavy

ASKER

Hi leakim

Unfortunately, I can't use data attributes as I am using IE 8 and it has no HTML5 support. I have to rely on native javascript.
You can still use them, just means your page won't validate.
Thank you...I will give that a try.