• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Two problems: (1) Prompt box doesn't open and (2) when I get the ordered list to print, the "if (i == 5)" also prints along with the list.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Secure Area</title>
<link rel="stylesheet" href="Styling.css">
<script src="script.js"></script>
</head>
<body>

<!--For the screen's top line. Create a 3-cell container. In cell 1, put a link to my home page. In cell 2, put the page's title. Leave cell 3 empty. -->

<div id="textbox">
<div class="left-cell"><a href style="color:red"="index.html">Home</a></div>
<div class="middle-cell">Secure Area</div>
<div class="right-cell">&emsp;</div>
</div>
<br /><br /><br />

<script>

let i=0, PC='' // Initialize variable i (the counter) and PC.

// Begin a Do ... While i<3 loop.

do { PC=prompt("Please enter your passcode exactly as I gave it to you.","");

// Test PC to see what the LA did.

if (PC == null) (window.location = 'index.html') // He hit "Cancel"; so return to home page.

// He hit the OK button. Did he enter the correct passcode?

if (PC == "XYZ") (i = 5) // Correct passcode entered; so set i = 5.

else (++i) // Incorrect passcode entered; so increment i

}

while i<3

</script>

<!-- We're again in HTML land. Control arrives here only if i==5 (a good passcode was entered) or i==3 (bad passcode entered 3 times). -->

if (i == 5) { <p>Please select the manuscript in which you're interested:</p>
<ol>
<li><a href="EinsteinBook.html">Einstein--The Man and His Thoughts</a></li>
<li><a href="EduAndItsMgtBook.html">Education and Its Management</a></li>
<li><a href="SuccessAndFormalSchoolingBook.html">Success and Formal Schooling</a></li>
</ol>
}

<!-- Control arrives here only if i == 3. Bad passcode entered 3 times.-->

alert("After 3 tries, you have not entered the correct passcode. Hit the OK button, and you will be returned to this site's home page.")

(window.location = 'index.html') <!-- Return him to the home page. -->

</body>
</html>
 

Antero360

King Coder
Staff Team
Security Analyst
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Secure Area</title>
<link rel="stylesheet" href="Styling.css">
<script src="script.js"></script>
</head>
<body>

<!--For the screen's top line. Create a 3-cell container. In cell 1, put a link to my home page. In cell 2, put the page's title. Leave cell 3 empty. -->

<div id="textbox">
<div class="left-cell"><a href style="color:red"="index.html">Home</a></div>
<div class="middle-cell">Secure Area</div>
<div class="right-cell">&emsp;</div>
</div>
<br /><br /><br />

<script>

let i=0, PC='' // Initialize variable i (the counter) and PC.

// Begin a Do ... While i<3 loop.

do { PC=prompt("Please enter your passcode exactly as I gave it to you.","");

// Test PC to see what the LA did.

if (PC == null) (window.location = 'index.html') // He hit "Cancel"; so return to home page.

// He hit the OK button. Did he enter the correct passcode?

if (PC == "XYZ") (i = 5) // Correct passcode entered; so set i = 5.

else (++i) // Incorrect passcode entered; so increment i

}

while i<3

</script>

<!-- We're again in HTML land. Control arrives here only if i==5 (a good passcode was entered) or i==3 (bad passcode entered 3 times). -->

if (i == 5) { <p>Please select the manuscript in which you're interested:</p>
<ol>
<li><a href="EinsteinBook.html">Einstein--The Man and His Thoughts</a></li>
<li><a href="EduAndItsMgtBook.html">Education and Its Management</a></li>
<li><a href="SuccessAndFormalSchoolingBook.html">Success and Formal Schooling</a></li>
</ol>
}

<!-- Control arrives here only if i == 3. Bad passcode entered 3 times.-->

alert("After 3 tries, you have not entered the correct passcode. Hit the OK button, and you will be returned to this site's home page.")

(window.location = 'index.html') <!-- Return him to the home page. -->

</body>
</html>
Hi there,
Part of the issue is that you need to encapsulate any javascript you wish to run with the script tag.
 

Antero360

King Coder
Staff Team
Security Analyst
You can get your code to work in 2 different ways:
1) you save all your javascript code in a separate file, and link to that file in the head tag

OR

2) you can write your code "in line" with the html, but you need to have it within a script tag.
 
Yes, however, javascript code on any part of a webpage needs to be inside the script tag in order for it to run.
The code below, will run, because it is contained within the script tag
View attachment 1771


The code below will not run because it is not contained within a script tag
View attachment 1772
You are right about the 2nd problem, but that does not solve the first problem, which is that the prompt box does not open. The prompt-box code is within the script tags.
 

cbreemer

King Coder
First, please always post your code in proper BBCode tags. It's hard to read if you don't.
Second, I get the feeling you think that HTML and JS can be freely mixed to interact with each other. But something like this


Code:
<!-- We're again in HTML land. Control arrives here only if i==5 (a good passcode was entered) or i==3 (bad passcode entered 3 times). -->
if (i == 5) { <p>Please select the manuscript in which you're interested:</p>
<ol>
<li><a href="EinsteinBook.html">Einstein--The Man and His Thoughts</a></li>
<li><a href="EduAndItsMgtBook.html">Education and Its Management</a></li>
<li><a href="SuccessAndFormalSchoolingBook.html">Success and Formal Schooling</a></li>
</ol>
}
is absolutely not going to work. Even apart from the syntax, you cannot magically pop up some static HTML from within a dynamic script. If you want to work like this, consider using PHP. Otherwise, if you want your script to add HTML conditionally on the client side, you'll need to use the DOM, either to create the elements dynamically and add them to your document, or to make static but previously hidden elements visible. I hope that makes sense.
 
Top