Solved

set src attribute to a javascript function

Posted on 2014-10-18
7
18 Views
Last Modified: 2016-05-09
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!
0
Comment
Question by:Techsavy
  • 2
  • 2
7 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40389604
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.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40389608
<img id="visionimage" data-title="visionimage" data-name="vision.jpg" alt="vision" /><span>Vision</span>

Open in new window


$(document).ready(function() {
      var siteUrl = getSiteUrl();
      $("img[data-src]").each(function() {
           var imagetitle = $(this).data("title"); // visionimage
           var name = $(this).data("name"); // vision.jpg
           var imageUrl = siteUrl + "/SiteAssets/images/" + imagetitle;
           $(this).attr("src", imageUrl);
      });
});

Open in new window

0
 

Author Comment

by:Techsavy
ID: 40389635
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.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40390017
You can still use them, just means your page won't validate.
0
 

Author Comment

by:Techsavy
ID: 40417251
Thank you...I will give that a try.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question