Probably a news ticker is one of the simplest and most common applications that we've seen on websites. Certainly, there are many ways to create such a thing, from server-side based solutions to JavaScript or Flash programs; choices are numerous. However, the main advantage of creating this news ticker (or a similar application) is that we don't need to work directly with JavaScript arrays to update information, since the PHP function handles the process in a transparent way for us. Any updating task is performed at a text file level, even without harming the structural HTML markup. It sounds like I'm marketing the program to you! Not really. Let's start defining the corresponding JavaScript functions to make the news ticker work. First, we need a file to store our news. Let's create a simple text file, which contains the news that we need to display. The "news.txt" file would look like this:
Despite the fact that our breaking news stories are rather funny (except for the last one…just kidding!), the file is extremely simplistic. So, the next step consists of generating the JavaScript array and storing in it the file contents. How do we do it? Here, we invoke our "createJavaScript()" function with the proper parameters:
With this single line, we've created the "news" JavaScript array, directly storing the contents of the "news.txt" file into it. Having populated the array with our funny news, it's time to look at the necessary JavaScript functions. If you're not very familiar with the DOM (Document Object Model) and its methods, don't worry. The functions are pretty easy to grasp. Trust me.
blog comments powered by Disqus |