Solved

set src attribute to a javascript function

Posted on 2014-10-18
7
15 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

772 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