Part 4 Mobile Apps
Part 4 Mobile Apps
1
Design simple app with Dreamweaver CS6 – Part 4
(Embed JavaScript to calculate user rating)
A) CREATE A NEW JAVASCRIPT FILE
1. Open DW and make sure that your site in the Site panel is the same site that you have worked before (during our lab on 23 April 2017)
2. Double click file index.html from your site panel to open it
3. Now, we are going to make a JavaScript file.
4. Choose File>New>JavaScript and click Create button.
5. Save the JavaScript file as restaurantJS.js
6. Inside the restaurantJS.js, type the following code to create a pop-up menu
function functionRate() {
alert("I am an alert box!");
}
B) EMBED THE JAVASCRIPT FUNCTION INTO HTML FILE
1. Switch back your Source Code Panel to the index.html code. We need to embed the restaurantJS.js function into index.html.
2. Inside index.html Source Code Panel, find the code <head> ….. </head>
2
3. Inside the <head> ….. </head> code, type:
<script type="text/javascript" src="../restaurantJS.js"></script>
While your’re typing the code, DW will help you to find the JavaScript file like the image below:
4. Now, we are going to use the JavaScript function to the Submit button
5. Look at your source code; scroll down until you find page pageRate and the code shows as below:
<div data-role="page" id=" pageRate ">
6. Scroll down the pageRate page until you find the Submit Button Code; something like below:
<!--Start of Submit Button-->
<a href="#" data-role="button" data-icon="arrow-l" data-iconpos="left">Submit</a>
<!--End of Submit Button-->
7. Add <button> tag at the beginning of the <a href > and add </button> tag after </a> tag. Your new
code will be like this:
<!--Start of Submit Button-->
<button> <a href="#" data-role="button" data-icon="arrow-l" dataiconpos="
left">Submit</a> </button>
<!--End of Submit Button-->
8. Place your cursor within the <button> and type onclick=" functionRate()". You’re overall code for
the Submit button will be like below.
<!--Start of Submit Button-->
<button onclick="functionRate()"><a href="#" data-role="button" data-icon="arrow-r" dataiconpos="
right">Submit</a> </button>
<!--End of Submit Button-->
9. Save your file. Now you can test your app in the Browser for the Page Rate to see the pop-up menu
from the JavaScript code.
3
10. Your Rate Page should be like below. You can see the pop-up menu when you click the Submit button.
C) CREATE JAVASCRIPT FUNCTION FOR SELECT MENU
1. Choose the restaurantJS.js tab on the Source Code Panel
2. Type the following lines to add function into the restaurantJS.js so that you code will be like this.
function functionRate() {
var selectOption = document.getElementById("selectmenu"); //get value from the Select Menu
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text; // get the text
alert("You have selected: " +selectedRestaurant+" restaurant");
}
Description of the codes:
var selectOption = document.getElementById("selectmenu");
The code will ask Browser to get element from Select Menu whose id is "selectmenu”. Then the
element will be assigned to a variable named selectOption
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text;
The code will get all elements form the variable selectOption then assign the text value to a new
variable named selectedRestaurant
alert("You have selected: " +selectedRestaurant+" restaurant");
4
The code will display the text “You have selected: " +selectedRestaurant+" restaurant” where
+selectedRestaurant+ is the content of selectedRestaurant variable
3. Save you JavaScript and html file. Open the html file in the Browser.
4. Choose any restaurant name from the Select Menu the click Submit Button. You should get the
message like this:
D) CALCULATE THE AVERAGE RATE FROM THE SLIDER
1. Open the restaurantJS.js in the Source Code Panel
2. Add the following line just above the alert code in the functionRate
var valFoodTaste = document.getElementById("sliderFoodTaste").value;
var valFoodPrice = document.getElementById("sliderFoodPrice").value;
var valCustomer = document.getElementById("sliderCustomer").value;
var valRestaurant = document.getElementById("sliderRestaurant").value;
var valOverall = document.getElementById("sliderOverall").value;
Your overall code will be like this:
function functionRate() {
var selectOption = document.getElementById("selectmenu"); //get value from the Select Menu
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text; // get the text
var valFoodTaste = document.getElementById("sliderFoodTaste").value;
var valFoodPrice = document.getElementById("sliderFoodPrice").value;
var valCustomer = document.getElementById("sliderCustomer").value;
var valRestaurant = document.getElementById("sliderRestaurant").value;
var valOverall = document.getElementById("sliderOverall").value;
alert("You have selected: " +selectedRestaurant+" restaurant");
}
5
3. Add the variable to calculate the average by typing this code:
var average = (parseInt(valFoodTaste) +parseInt(valFoodPrice) +parseInt(valCustomer)
+parseInt(valRestaurant) + parseInt(valOverall))/5;
This code will convert each value from step 3 from string to integer using parseInt function
Then all values will be divided with 5 to get the average
The average value will be assigned to variable average
4. To display the score in the pop-up menu; change the alert code to be like below:
alert("You have selected: " +selectedRestaurant+" restaurant: "+"\n"+ "Averate rate score: "+average);
"+"\n"+ : it will add a new text line in the pop-up menu
Averate rate score: "+average : it will display the average score from the sliders value
5. Your overall code will be:
function functionRate() {
var selectOption = document.getElementById("selectmenu"); //get the value from the Select
Menu
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text; //get the text
from the Select Menu
var valFoodTaste = document.getElementById("sliderFoodTaste").value;
var valFoodPrice = document.getElementById("sliderFoodPrice").value;
var valCustomer = document.getElementById("sliderCustomer").value;
var valRestaurant = document.getElementById("sliderRestaurant").value;
var valOverall = document.getElementById("sliderOverall").value;
var average = (parseInt(valFoodTaste)+ parseInt(valFoodPrice)+ parseInt(valCustomer)+
parseInt(valRestaurant)+ parseInt(valOverall))/5;
alert("You have selected: " +selectedRestaurant+" restaurant: "+"\n"+ "Averate rate score:
"+average);
}
6. Save you JavaScript and html file. Open the html file in the Browser.
7. Choose any restaurant name from the Select Menu the click Submit Button. You should get the
message like this:
6
F) ADD CONDITIONAL IF..ELSE IF..ELSE STATEMENT FOR THE AVERAGE RATE
1. We are going to categories the satisfaction level based on variable average
2. Add the following if..else is..else bold codes between var average and alert () code so that you
functionRate will be like this:
var average = (parseInt(valFoodTaste)+ parseInt(valFoodPrice)+ parseInt(valCustomer)+
parseInt(valRestaurant)+parseInt(valOverall))/5;
var satisfyLevel;
if (average<5) {
satisfyLevel = "Poor";
} else if (average>5 && average<8 ) {
satisfyLevel = "Average";
} else if (average>8 ) {
satisfyLevel = "Excellent";
} else {
satisfyLevel = "Please slide the satisfaction slider;
}
alert("You have selected: "…
Description:
var satisfyLevel : create a new variable named satisfyLevel
IF..ELSE IF..ELSE statement: take the average value and categorized the value into 3 categories.
Category 1 for Poor rating, Category 2 for Average rating and Category 3 for Excellent rating.
7
3. Modify you alert () code to be like this:
alert("You have selected: " +selectedRestaurant+" restaurant"+"\n"+ "Averate rate score:
"+average+"\n"+"Satisfaction Level: "+satisfyLevel+"\n"+"Thank you for your rating");
4. Save you JavaScript and html file. Open the html file in the Browser.
5. Choose any restaurant name from the Select Menu the click Submit Button. You should get the
message like this:
1
Design simple app with Dreamweaver CS6 – Part 4
(Embed JavaScript to calculate user rating)
A) CREATE A NEW JAVASCRIPT FILE
1. Open DW and make sure that your site in the Site panel is the same site that you have worked before (during our lab on 23 April 2017)
2. Double click file index.html from your site panel to open it
3. Now, we are going to make a JavaScript file.
4. Choose File>New>JavaScript and click Create button.
5. Save the JavaScript file as restaurantJS.js
6. Inside the restaurantJS.js, type the following code to create a pop-up menu
function functionRate() {
alert("I am an alert box!");
}
B) EMBED THE JAVASCRIPT FUNCTION INTO HTML FILE
1. Switch back your Source Code Panel to the index.html code. We need to embed the restaurantJS.js function into index.html.
2. Inside index.html Source Code Panel, find the code <head> ….. </head>
2
3. Inside the <head> ….. </head> code, type:
<script type="text/javascript" src="../restaurantJS.js"></script>
While your’re typing the code, DW will help you to find the JavaScript file like the image below:
4. Now, we are going to use the JavaScript function to the Submit button
5. Look at your source code; scroll down until you find page pageRate and the code shows as below:
<div data-role="page" id=" pageRate ">
6. Scroll down the pageRate page until you find the Submit Button Code; something like below:
<!--Start of Submit Button-->
<a href="#" data-role="button" data-icon="arrow-l" data-iconpos="left">Submit</a>
<!--End of Submit Button-->
7. Add <button> tag at the beginning of the <a href > and add </button> tag after </a> tag. Your new
code will be like this:
<!--Start of Submit Button-->
<button> <a href="#" data-role="button" data-icon="arrow-l" dataiconpos="
left">Submit</a> </button>
<!--End of Submit Button-->
8. Place your cursor within the <button> and type onclick=" functionRate()". You’re overall code for
the Submit button will be like below.
<!--Start of Submit Button-->
<button onclick="functionRate()"><a href="#" data-role="button" data-icon="arrow-r" dataiconpos="
right">Submit</a> </button>
<!--End of Submit Button-->
9. Save your file. Now you can test your app in the Browser for the Page Rate to see the pop-up menu
from the JavaScript code.
3
10. Your Rate Page should be like below. You can see the pop-up menu when you click the Submit button.
C) CREATE JAVASCRIPT FUNCTION FOR SELECT MENU
1. Choose the restaurantJS.js tab on the Source Code Panel
2. Type the following lines to add function into the restaurantJS.js so that you code will be like this.
function functionRate() {
var selectOption = document.getElementById("selectmenu"); //get value from the Select Menu
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text; // get the text
alert("You have selected: " +selectedRestaurant+" restaurant");
}
Description of the codes:
var selectOption = document.getElementById("selectmenu");
The code will ask Browser to get element from Select Menu whose id is "selectmenu”. Then the
element will be assigned to a variable named selectOption
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text;
The code will get all elements form the variable selectOption then assign the text value to a new
variable named selectedRestaurant
alert("You have selected: " +selectedRestaurant+" restaurant");
4
The code will display the text “You have selected: " +selectedRestaurant+" restaurant” where
+selectedRestaurant+ is the content of selectedRestaurant variable
3. Save you JavaScript and html file. Open the html file in the Browser.
4. Choose any restaurant name from the Select Menu the click Submit Button. You should get the
message like this:
D) CALCULATE THE AVERAGE RATE FROM THE SLIDER
1. Open the restaurantJS.js in the Source Code Panel
2. Add the following line just above the alert code in the functionRate
var valFoodTaste = document.getElementById("sliderFoodTaste").value;
var valFoodPrice = document.getElementById("sliderFoodPrice").value;
var valCustomer = document.getElementById("sliderCustomer").value;
var valRestaurant = document.getElementById("sliderRestaurant").value;
var valOverall = document.getElementById("sliderOverall").value;
Your overall code will be like this:
function functionRate() {
var selectOption = document.getElementById("selectmenu"); //get value from the Select Menu
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text; // get the text
var valFoodTaste = document.getElementById("sliderFoodTaste").value;
var valFoodPrice = document.getElementById("sliderFoodPrice").value;
var valCustomer = document.getElementById("sliderCustomer").value;
var valRestaurant = document.getElementById("sliderRestaurant").value;
var valOverall = document.getElementById("sliderOverall").value;
alert("You have selected: " +selectedRestaurant+" restaurant");
}
5
3. Add the variable to calculate the average by typing this code:
var average = (parseInt(valFoodTaste) +parseInt(valFoodPrice) +parseInt(valCustomer)
+parseInt(valRestaurant) + parseInt(valOverall))/5;
This code will convert each value from step 3 from string to integer using parseInt function
Then all values will be divided with 5 to get the average
The average value will be assigned to variable average
4. To display the score in the pop-up menu; change the alert code to be like below:
alert("You have selected: " +selectedRestaurant+" restaurant: "+"\n"+ "Averate rate score: "+average);
"+"\n"+ : it will add a new text line in the pop-up menu
Averate rate score: "+average : it will display the average score from the sliders value
5. Your overall code will be:
function functionRate() {
var selectOption = document.getElementById("selectmenu"); //get the value from the Select
Menu
var selectedRestaurant = selectOption .options[selectOption.selectedIndex].text; //get the text
from the Select Menu
var valFoodTaste = document.getElementById("sliderFoodTaste").value;
var valFoodPrice = document.getElementById("sliderFoodPrice").value;
var valCustomer = document.getElementById("sliderCustomer").value;
var valRestaurant = document.getElementById("sliderRestaurant").value;
var valOverall = document.getElementById("sliderOverall").value;
var average = (parseInt(valFoodTaste)+ parseInt(valFoodPrice)+ parseInt(valCustomer)+
parseInt(valRestaurant)+ parseInt(valOverall))/5;
alert("You have selected: " +selectedRestaurant+" restaurant: "+"\n"+ "Averate rate score:
"+average);
}
6. Save you JavaScript and html file. Open the html file in the Browser.
7. Choose any restaurant name from the Select Menu the click Submit Button. You should get the
message like this:
6
F) ADD CONDITIONAL IF..ELSE IF..ELSE STATEMENT FOR THE AVERAGE RATE
1. We are going to categories the satisfaction level based on variable average
2. Add the following if..else is..else bold codes between var average and alert () code so that you
functionRate will be like this:
var average = (parseInt(valFoodTaste)+ parseInt(valFoodPrice)+ parseInt(valCustomer)+
parseInt(valRestaurant)+parseInt(valOverall))/5;
var satisfyLevel;
if (average<5) {
satisfyLevel = "Poor";
} else if (average>5 && average<8 ) {
satisfyLevel = "Average";
} else if (average>8 ) {
satisfyLevel = "Excellent";
} else {
satisfyLevel = "Please slide the satisfaction slider;
}
alert("You have selected: "…
Description:
var satisfyLevel : create a new variable named satisfyLevel
IF..ELSE IF..ELSE statement: take the average value and categorized the value into 3 categories.
Category 1 for Poor rating, Category 2 for Average rating and Category 3 for Excellent rating.
7
3. Modify you alert () code to be like this:
alert("You have selected: " +selectedRestaurant+" restaurant"+"\n"+ "Averate rate score:
"+average+"\n"+"Satisfaction Level: "+satisfyLevel+"\n"+"Thank you for your rating");
4. Save you JavaScript and html file. Open the html file in the Browser.
5. Choose any restaurant name from the Select Menu the click Submit Button. You should get the
message like this:
Comments
Post a Comment