Solved

set src attribute to a javascript function

Posted on 2014-10-18
7
20 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

738 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