admin管理员组文章数量:1356850
I'm fiddling around with a pretend "Logon screen" but am having trouble when it es to redirecting the page.
I've tried a couple of different suggestions that I've found on the site, but so far nothing has worked.
I honestly think I'm just doing it wrong.
Any advice?
HTML
<div class="title"><h1>Webby Site</h1></div>
<div class="login-page">
<div class="form">
<form class="login-form">
<input id="User" type="text" placeholder="username"/>
<input id="PWord" type="password" placeholder="password"/>
<button class="button">Login</button>
<p class="message">Not registered? <a>Create an account</a></p>
</form>
</div>
</div>
JS
$(document).ready(function(){
$('.button').click(function(){
if ($('input#User').val() === "1" && $('input#PWord').val() === "1"){
document.location.href = "www.yoursite";
} else {
alert('Incorrect Account Details');
}
});
});
Code I've tried (in the same area as document.location.href
)
document.location
window.location
window.location.href
location.href
I'm fiddling around with a pretend "Logon screen" but am having trouble when it es to redirecting the page.
I've tried a couple of different suggestions that I've found on the site, but so far nothing has worked.
I honestly think I'm just doing it wrong.
Any advice?
HTML
<div class="title"><h1>Webby Site</h1></div>
<div class="login-page">
<div class="form">
<form class="login-form">
<input id="User" type="text" placeholder="username"/>
<input id="PWord" type="password" placeholder="password"/>
<button class="button">Login</button>
<p class="message">Not registered? <a>Create an account</a></p>
</form>
</div>
</div>
JS
$(document).ready(function(){
$('.button').click(function(){
if ($('input#User').val() === "1" && $('input#PWord').val() === "1"){
document.location.href = "www.yoursite.";
} else {
alert('Incorrect Account Details');
}
});
});
Code I've tried (in the same area as document.location.href
)
document.location
window.location
window.location.href
location.href
- Could you reproduce this problem at JSFiddle, please? – Yeldar Kurmangaliyev Commented Oct 5, 2016 at 3:11
- @YeldarKurmangaliyev unfortunately, while it works okay otherwise in Chrome, it doesn't work at all in JSFiddle. I get "{"error": "Please use POST request"}". But I can make one anyway, if it would help? – Asteria Commented Oct 5, 2016 at 3:29
2 Answers
Reset to default 4By default, button
acts like input type="submit"
, so it's trying to submit your form. Use event.preventDefault()
to avoid this behaviour.
Correct way to redirect is setting window.location.href
.
Also, I strongly remend not to try client-side authentification, because anyone could just read login and password in your js file.
For HTML, you can use <button onclick="location.href='url'">text</button>
You can also use <button onclick="window.location.replace(url)">text</button>
本文标签: Javascript Redirect page onclickStack Overflow
版权声明:本文标题:Javascript: Redirect page onclick - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744057620a2583540.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论