cancel
Showing results for 
Search instead for 
Did you mean: 

Adding an Image in HTML doc using JavaScript in Alfresco

juilee
Champ in-the-making
Champ in-the-making
I have an image url defined in my javascript code as:
var url = "http://127.0.0.1:8081/share/proxy/alfresco/api/node/content/'+urlStr.substring(4);
I have to pass this as src attribute to an image:

var content = "";
content += "<img src=\"url\">";

But this doesn't work.
I also tried inserting image using document.createElement("img"); and var img = new Image(); but javascript is not able to recognize the createElement() method or the Image() object.
Can anyone suggest how an image can be added to the HTML doc using Javascript in Alfresco?
16 REPLIES 16

juilee
Champ in-the-making
Champ in-the-making
The url path is correct as I could make a hyperlink with it and that works fine. My port is 8081 configured as I am running using the alfresco development environment. I think there is some issue with passing a string to JavaScript. It takes the string literally rather than replacing it with the actual contents of the string. Also, I tried creating img tag using document.createElement("img"); but the error log says cannot find createElement(). Any suggestions on a workaround this problem. Thanks a lot!

aaditvmajmudar
Confirmed Champ
Confirmed Champ
I am not sure if I have understood correctly or not but as per my understanding if you are facing some problem to render image with javascript then I had same requirement to display image in div element from javascript and I have used in same way as I mentioned like,

var imageUrl = Alfresco.constants.PROXY_URI+"api/node/content/workspace/SpacesStore/"+{UUID of image content}+"/"+{Name of the image};

elCell.innerHTML = '<img src="'+imageUrl+'" width = "65px" height = "35px"/>';

Hope this helps.

Should


"<img src=\"'+res+'\" width = \"65px\" height = \"35px\"/>";


not be


"<img src=\"+res+\" width = \"65px\" height = \"35px\"/>";



juilee
Champ in-the-making
Champ in-the-making
Hi Aadit when I use innerHTML() and getElementById() function, I get an error saying, cannot recognize those functions. Most of the normal javascript functions won't work. Are there any settings to enable them in Alfresco?
Hi Bob, I had tried that too but still no luck.

Thanks!

aaditvmajmudar
Confirmed Champ
Confirmed Champ
I think you are not coding in client side JS file.

You are writing these code stuff in one of the webscript JS file OR Action JS file??

Hi Aadit, sorry but I don't understand you. I am writing this javascript file in the Script folder of Data Dictionary and executing it using a rule on one of my folders. I wanted to invoke it using a simple rule so didn't use webscript but directly executing JavaScript. Thanks again for your time and looking into this.

juilee
Champ in-the-making
Champ in-the-making
Finally, writing it this way worked for me:

'<img src=\"'+res+'\" width = \"150px\" height = \"100px\" >'