?
Solved

Conditional statement question

Posted on 2013-05-23
2
Medium Priority
?
221 Views
Last Modified: 2013-05-23
I have a scrolling image which needs to be smaller for iPhone.  Here is the code:
<ul id=scroller>
if ((navigator.userAgent.indexOf('iPhone') != -1)) {
document.write('<li><img src="images/1.jpg" Alt="" ></li>');
} else
{document.write('<li><img src="images/2.jpg" Alt="" ></li>');
}
</ul>

If I insert <script type="text/javascript"> above the opening <ul> tag, and </script> before the closing </ul>, then the image doesn't scroll.  If I insert it after the opening <ul> tag, I get an error that <ul> cannot contain a script tag.  If I leave it out, the page scrolls, but the same image is loaded, regardless of the user agent.  Please advise.
0
Comment
Question by:ddantes
[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 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 1000 total points
ID: 39192912
Try something like this:

<ul id=scroller>
     <li><img id="image1" src="about:blank" Alt="" /></li>
</ul>

<script>
if ((navigator.userAgent.indexOf('iPhone') != -1)) {
    document.getElementById("image1").src="images/1.jpg";
} else {
    document.getElementById("image1").src="images/2.jpg";
}
</script>

Open in new window


Basically, you don't use Javascript to actually write the HTML itself. Instead, you use Javascript to programatically change the value of the "src" attribute.

The script could go directly inline on the page, or it could go into your in <head> tag in an onload event.
0
 

Author Comment

by:ddantes
ID: 39192934
Thank you, that really helped.
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

777 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