Get your CodeRanch badge!*
The moose likes HTML, CSS and JavaScript and the fly likes Servlet returns snippet but image does not appear when run on cell phone Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Servlet returns snippet but image does not appear when run on cell phone" Watch "Servlet returns snippet but image does not appear when run on cell phone" New topic
Author

Servlet returns snippet but image does not appear when run on cell phone

Dave Anderson
Greenhorn

Joined: Nov 09, 2009
Posts: 29
I have an AJAX application that calls Apache Tomcat.

I have one problem that I cannot solve and need help on.

One of the application's jobs is to call the servlet with a message to get an image name. Once the image name is retrieved, it is loaded into a snippet of HTML as follows:

<img id='myimage' src='http://www. ... /images/nameThatWasFound.jpg' />

The servlet then returns the HTML snippet, and the application places it in an element's id as follows:

document.getElementById("myimage").innerHTML = theHTMLReturned

This works fine for a desktop browser such as Chrome and IE, but when I call it from a mobile phone,
the image does not display. If I tap the phone's screen however, then the image will appear.

I have tried to do the following to get the image to refresh, but the element id "myimage" is null, and it cannot be accessed for some reason I do not understand:


var el = document.getElementById('myimage');
el.setAttribute('src', el.getAttribute('src') + '?m='+Math.random());


I cannot understand why the element 'myimage' is null first of all, and secondly why the image will not load for display on the phone.

Any ideas? Doesn't work on a cell phone (Android).

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

If you are just trying to swap out the image, why are you not just changing its src attribute?

And with regards to:
<img> elements don't have inner HTML so I'm surprised the browsers are doing anything correctly at all.

What exactly is the nature of theHTMLReturned? Whatever it is, this isn't the proper way to be manipulating the DOM and it's not surprising that it's causing issues.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dave Anderson
Greenhorn

Joined: Nov 09, 2009
Posts: 29
My mistake Bear:
document.getElementById("myimage").innerHTML = theHTMLReturned SHOULD READ
document.getElementById("loadingSection").innerHTML = theHTMLReturned

"loading section" is where the generated HTML is loaded (this would be the inner HTLM for a <div> or <span> tag).

I'll look in the HTML section then for other replies.

Dave Anderson
Greenhorn

Joined: Nov 09, 2009
Posts: 29
After reflection, I realized I can retrieve the name of image from the returned <img> tag in "theHTMLReturned", and as a result do the following to reload the image:

// Get the image file name, then set the 'src' attribute for the element
var s = document.getElementById('myimage')
s.setAttribute('src', '/images/theActualImageFilename.jpg')

Thanks for the help as always.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet returns snippet but image does not appear when run on cell phone
 
Similar Threads
Dynamically added form fields cannot access from request access
Ajax code getting null value
Using an alt attribute with HTML elements
Setting background image of table column
YUI listener problem