Solved

Conditional statement question

Posted on 2013-05-23
2
212 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable Security Alert  popup in Winforms  embedded webbrowser. 1 30
Why a span is lower 2 20
Getting Random number with min and max value. 2 28
innerHTML 7 20
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
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.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

810 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