• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic