目的:点击按钮弹出新窗口,新窗口中的动作执行完毕后,原页面不刷新数据返回。
index.html
主页面,点击按钮时弹出新窗口
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 id="i1">无所谓</h1>
<a href="#" onclick="popUp('http://www.oldboyedu.com')">点我点我</a>
<script>
function popupCallback(text) {
document.getElementById('i1').innerHTML = text;
}
function popUp(url) {
window.open( '/pop/', '/pop/' ,"status=1, height:500, width:600, toolbar=0, resizeable=0");
}
</script>
</body>
</html>
pop.html
弹出窗口
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post">
{% csrf_token %}
<input type="text" name="user">
<input type="submit" value="保存">
</form>
</body>
</html>
pop_response.html
新窗口中执行动作的响应页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>正在关闭</h1>
<script>
(function () {
// 可以调用popup原页面的一个函数
opener.popupCallback("{{ user }}");
window.close();
})()
</script>
</body>
</html>
urls.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', views.index),
url(r'^pop/', views.pop),
]
views.py
def index(request):
return render(request,'index.html')
def pop(request):
if request.method == "GET":
return render(request, 'pop.html')
else:
user = request.POST.get('user')
return render(request,'pop_response.html',{'user':user})