私は半単純な問題があります。入力欄に名前を入力するようにユーザーに指示する必要があります。名前を入力すると、ページはHello [ユーザー名]という別のページに切り替わり、コンテンツも表示されます。ここでは、今別のjsファイルから関数を呼び出してテキストをhtmlページに挿入する方法
function menuPage() {
var firstName = document.getElementById("firstName").value;
if (firstName.trim() && firstName != "") {
document.getElementById("username").innerHTML = firstName;
window.location.replace("menu.html")
} else {
alert("Please put in your first name.")
}
}
:付随するJavaScriptファイル(name.js)ここで
<body>
<form id="myForm" name="myForm">
<table class="input">
<tr>
<label>
<h1>Enter your firstname:</h1>
</label>
<td>
<input type="text" id="firstName" class="form-control" autofocus>
</td>
<td>
<input type="button" value="Submit" onclick="menuPage();" class="btn btn-primary btn-lg">
</td>
</tr>
<tr>
<td id="username">
</td>
</tr>
</table>
</form>
<script src="name.js"></script>
<script src="ie10-viewport-bug-workaround.js"></script>
</body>
されています。ここでは
は、最初の名前を尋ねる私の最初のhtmlファイル(名.html)でありますユーザが名前を入力すると表示される新しいhtmlページ(menu.html)です:<div class="jumbotron">
<div class="container">
<div>
<h1 class="display-3">Hello!</h1>
<h1 id="username"></h1>
<p>This is your weekly meal to cook! Enjoy!!</p>
</p>
<h2 class="quote"><span class="words"></span></h2>
<button class="btn btn-success btn-sm" value="Press Me to get your Weekly Menu">Change Meal</button>
</div>
</div>
</div>
<script src="name.js"></script>
<script src="menu.js"></script>
最後に、付属のJSファイル(menu.js):
$(document).ready(function() {
weeklyMenu();
function weeklyMenu() {
var menu = ["meatloaf", "albondigas", "enchiladas", "lasgna", "chicken, mash potatoes, mac & cheese", "turkey burgers, sweet potatoes fries", "stuffed peppers", "french soup"];
var randomMenu = menu[Math.floor(Math.random() * menu.length)];
var splitMenu = randomMenu.split();
$('.words').text(splitMenu[0]);
}
$("button").on("click", function() {
weeklyMenu();
});
});
クライアントに関するすべての情報が失われます。それを行う1つの方法は、情報を輸送するために[クエステルリング](https://nl.wikipedia.org/wiki/Querystring)を使用することです。安全のためには、あなたは[encode]する必要があります(http://stackoverflow.com/questions/597028/how-can-encode-a-query-string-so-that-it-is-the-value-of-another- query-string-in)です。他のページでは、[読み込み](https://gomakethings.com/how-to-get-the-value-of-a-querystring-with-native-javascript/)を実行し、[page load]の間にクエリ文字列をデコードします。 (http://stackoverflow.com/questions/3842614/how-do-i-call-a-javascript-function-on-page-load)。 –