Solved

Conditional statement question

Posted on 2013-05-23
2
204 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
2 Comments
 
LVL 31

Accepted Solution

by:
Frosty555 earned 250 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now