New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Declaring global variables in JavaScript

From: Set a Marquee to Autoplay with jQuery and Dreamweaver

Video: Declaring global variables in JavaScript

Now that we're ready to modify the JavaScript file from the previous course, let's open up the marquee.js in Dreamweaver or switch over to that open tab. And before the (document).ready in this particular script, where we actually setup the marquee, let's hit a few Returns and let's set up the five variables that we need to access to make the autoplay function properly. So this is the global area inside of the JavaScript. We're going to start by typing var for variable. I'm going to start by typing currentPanel. We're going to set this equal to 1.

Declaring global variables in JavaScript

Now that we're ready to modify the JavaScript file from the previous course, let's open up the marquee.js in Dreamweaver or switch over to that open tab. And before the (document).ready in this particular script, where we actually setup the marquee, let's hit a few Returns and let's set up the five variables that we need to access to make the autoplay function properly. So this is the global area inside of the JavaScript. We're going to start by typing var for variable. I'm going to start by typing currentPanel. We're going to set this equal to 1.

Next we're going to type variable totalPanels. We're going to set this equal to zero to start and then we'll have our jQuery actually generate the total number of panels by modifying the script below. Next we're going to set autoPlay. We're going to set this equal to True. Next we'll set timePassed. we'll set that equal to zero as well.

Next we'll type variable timeToChange. We're going to set this to 3. This is going to be three seconds, so that the marquee will play every three seconds. This will be too fast for our end project, but we wanted to set something quick while we're testing and debugging this so we can actually make sure that it plays properly and it doesn't take a lot of time to play through. Now inside of the (document).ready, we want to set up a few statements here that will send these variables over to the HTML page. These are going to be part of our debugging scripts. So I'm going to type a forward slash and an asterisk, type debug and then asterisk and forward slash. This is going to put a small comment in the beginning of this line.

And I'm going to put this debug comment before every time we send data over to the HTML, so that we can go back later and be able to remove these. So I'm going to start by typing a dollar sign, beginning and ending parenthesis. Inside of the parentheses we'll type two tick marks. I'm going to type autoPlay and this is the autoPlay class from the debug area in the HTML, not the variable that we have set up above here.

So we're going to set the autoPlay div, html, beginning and ending parentheses, semicolon, and then inside of the html we're going to put two ticks for a string literal, and then we're going to type autoPlay, which is just text, equals, space, and then I get the cursor outside of the tick mark, but before the ending parentheses, hit a plus sign, and now we need to access the global variables inside of this JavaScript. We are running inside of the function here, so in order to go out and get that global variable we're going to type window.autoPlay.

Again, by typing window here we can go out to the global script area. Now that we have the first debug statement set up, let's copy this. Command+C or Ctrl+C to copy. We're going to paste two, three more of these to give ourselves a total of four. We have five of them up here, but we're going to set the total panels a little bit later. So now Dreamweaver gives us a really nice way to select variable names and class names and element names. I'm going to come up here. I want to set the timePassed variable, it being the second one.

So I can come up here, double- click on timePassed, and then it will automatically select that item. Command+C or Ctrl+C to copy. Let's come down in the debug line here. Double-click on autoPlay. It will automatically highlight. I can hit Paste. I can double-click autoPlay here which is a string literal, and double-click autoPlay here which is a JavaScript variable. So this gives me again a really quick way to cut and paste the different values. Next, let's come up here and double-click timeToChange. Copy that. Let's paste that into the third spot and then let's come up and double-click currentPanel.

Let's come down and paste currentPanel on the fourth item here. At this point let's choose File > Save. So now at the beginning of the document we have our global five variables set up. We're going to set the state of these variables to debug in the HTML when we load the page. And in the next movie we're going to modify the existing script to get the total panels and the actual current panel based on the navigational item that was clicked.

Show transcript

This video is part of

Image for Set a Marquee to Autoplay with jQuery and Dreamweaver
 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.