POST/GET不刷新全部页面的方法

有些时候向网页服务器提交POST/GET数据时,不希望服务器返回的数据覆盖原页面内容,比如登陆页面,提交用户名和密码内容时。

以下提供了一种不需要jquery方法。主要是页面中添加一个iframe,设为隐藏。将表单frame的target属性指向这个iframe的name。然后为这个iframe添加onload事件函数。

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="GB2312">  
	<link rel="icon" href="data:image/ico;base64,aWNv">
    <title>登录</title>  
    <style>
    html{   
    width: 100%;   
    height: 100%;   
    overflow: hidden;   
    font-style: sans-serif;   
}   
 body{   
    width: 100%;   
    height: 100%;   
    font-family: 'Open Sans',sans-serif;   
    margin: 0;   
    background-color: #006dad;   
}   
#login{   
    position: absolute;   
    top: 40%;   
    left:50%;   
    margin: -150px 0 0 -150px;   
    width: 300px;   
    height: 300px;   
}   
#login h1{   
    color: #fff;   
    text-shadow:0 0 5px;   
    letter-spacing: 1px;   
    text-align: center;   
}   
h1{   
    font-size: 2em;   
    margin: 0.67em 0;   
}   
input{   
    width: 278px;   
    height: 18px;   
    margin-bottom: 10px;   
    outline: none;   
    padding: 10px;   
    font-size: 13px;   
    border-top: 1px solid #312E3D;   
    border-left: 1px solid #312E3D;   
    border-right: 1px solid #312E3D;   
    border-bottom: 1px solid #56536A;   
    border-radius: 4px;  
    color: #000000; 
    background-color: #ffffff;   
}   
.but{   
    width: 300px;   
    min-height: 20px;   
    display: block;   
    background-color: #4a77d4;   
    border: 1px solid #3762bc;   
    color: #fff;   
    padding: 9px 14px;   
    font-size: 15px;   
    line-height: normal;   
    border-radius: 5px;   
    margin: 0;   
}
    </style>
</head>  
<body>  
    <div id="login">
        <div style='width:300px;height:50px;text-align:center;'><h1>xxxxxxx</h1></div>
        <h1>登录</h1>
        <form method="post" target="hideIframe1">
            <input id='idUser' name='user' type="text" required="required" placeholder="用户名" ></input>
            <input type='password' id='idPassword' name='password'  required="required" placeholder="密码"></input>
			<button class="but" type="submit">登录</button>
		</form>
        <div style='width:300px;height:30px;margin-top:100px;text-align:center;color:white'>
        &copy; 2022 xxxxx科技有限公司</div>
		<iframe id="id_frame" name="hideIframe1" title="title_Iframe" style="display:none;"></iframe>
    </div>
	<script>
		function onloadFunc(){
			console.log("执行事件函数");
			var mybody = window.document.getElementById("id_frame").contentWindow.document.body;
			if(mybody){
				var txt = mybody.innerText;
				if(txt!=""){
					console.log("receive: " + txt);
					var o=eval('('+txt+')');
					if(o.en=="1")
						//{window.location.href="xx.html";}
						{alert("用户名和密码正确!");}
					else {alert("用户名或密码错误!");}
				}		
			}
			else{console.log("mybody 无效");}
		}
		var myiframe = window.document.getElementById("id_frame");
	    if(myiframe){myiframe.onload = onloadFunc;}
		else 		{console.log("myiframe 无效");}
    </script>
</body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

d1z1y1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值