Solved

Make embedded JavaScript function generic

Posted on 2014-02-03
4
370 Views
Last Modified: 2014-06-10
I have a Java Script function in a page markup fired by the load event to display rotating images in a Div.

I have other pages that I need rotating images on.

How do I write this function generically, so that I can place it in a separate .js file, and call it using the Div ID as a passed parameter?

<asp:Content ID="Content2" ContentPlaceHolderID="RightSidebarContent" Runat="Server">

    <script type="text/javascript" language="javascript">
        window.onload = function() {
            var rotator = document.getElementById("rotator");
            var images = rotator.getElementsByTagName("img");
            for (var i = 1; i < images.length; i++) {
                images[i].style.display = "none";
            }
            var counter = 1;
            setInterval(function() {
                for (var i = 0; i < images.length; i++) {
                    images[i].style.display = "none";
                }
                images[counter].style.display = "block";
                counter++;
                if (counter == images.length) {
                    counter = 0;
                }
            }, 2500);
        };
    </script>
    
    <div id="rotator">
        <img alt="" src="../images/MainRotating.jpg" />
        <img alt="" src="../images/AnnexRotating.jpg" style="display:none" />
        <img alt="" src="../images/NAnnexRotating.jpg" style="display:none"  />
        <img alt="" src="../images/R5Rotating.jpg" style="display:none" />
    </div>


</asp:Content>

Open in new window



Thanks
0
Comment
Question by:mbizup
[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
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 total points
ID: 39829755
<script type="text/javascript" src="rotate.js"></script>
<asp:Content ID="Content2" ContentPlaceHolderID="RightSidebarContent" Runat="Server">

    <script type="text/javascript" language="javascript">
        window.onload = function() {
            rotate('rotator');
        });
    </script>
    
    <div id="rotator">
        <img alt="" src="../images/MainRotating.jpg" />
        <img alt="" src="../images/AnnexRotating.jpg" style="display:none" />
        <img alt="" src="../images/NAnnexRotating.jpg" style="display:none"  />
        <img alt="" src="../images/R5Rotating.jpg" style="display:none" />
    </div>
</asp:Content>

Open in new window

Add your method to an external js file called (in this example) rotate.js
function rotate(elementID) {
    var rotator = document.getElementById(elementID);
    var images = rotator.getElementsByTagName("img");
    for (var i = 1; i < images.length; i++) {
        images[i].style.display = "none";
    }
    var counter = 1;
    setInterval(function() {
        for (var i = 0; i < images.length; i++) {
            images[i].style.display = "none";
        }
        images[counter].style.display = "block";
        counter++;
        if (counter == images.length) {
            counter = 0;
        }
    }, 2500);
}

Open in new window

0
 
LVL 61

Author Comment

by:mbizup
ID: 39829845
That's great,  Thanks!

I was actually very close on my own.  My issue seemed to have been in calling the function.

This is what I tried (without success):

window.onload =  rotatePics('rotator');


This, from your post is the correct syntax:

window.onload = function() { rotatePics('rotator'); }
0
 
LVL 61

Author Closing Comment

by:mbizup
ID: 39829846
Thanks!
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 39831776
@mizBizup : you were close indeed but you need a reference to a function. In this case the anonymous function which executes the rotatePics method.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

735 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