Solved

set src attribute to a javascript function

Posted on 2014-10-18
7
8 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
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Comment Utility
You can still use them, just means your page won't validate.
0
 

Author Comment

by:Techsavy
Comment Utility
Thank you...I will give that a try.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now