• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Dave Anderson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Anderson
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic