Solved

set src attribute to a javascript function

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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)

695 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