如何使用JavaScript在另一个jsp中包含一个jsp [英] How to include a jsp inside another jsp using javascript

查看:86
本文介绍了如何使用JavaScript在另一个jsp中包含一个jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮注销.单击注销后,我需要显示另一个页面.如何使用JavaScript做到这一点?谁能帮我吗?

I have a button logout. Once logout is clicked I need to show another page. How can I do this using JavaScript? Can anyone please help me?

我的代码:

<s:form name="LogoutAction"
                id="LogoutAction" action="logout">
    <span class="inlayField2">
    <s:a href="logout" cssClass="planTabHeader" id="logoutId"> <img src="../../KY/images/common/header/lock.png" alt="logout" style="border: none;background-color: transparent;" /> &nbsp;Log out</s:a></span>
    </s:form> 

我尝试过:

$('#logoutId').click(function(event) {

    $('#logoutdiv').load('ConfirmationPopup.jsp');
});

推荐答案

您可以使用ajax从第二个jsp获取html,然后附加到DOM或元素的innerHTML.

You could use ajax to get the html from the 2nd jsp and then append to the DOM or to the innerHTML of a element.

Main Page.jsp

Main Page.jsp

<span id=confirmSpan></span>
<script language="Javascript">
function xmlhttpPost(strURL, queryStr) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari, opera etc
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        alert("no ajax")
        return
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepageConfirm(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(queryStr);
}


function updatepageConfirm(str){
    document.getElementById("confirmSpan").innerHTML = str;
}
function logout1(){
xmlhttpPost("confirm.html");//http://sel2in.com/pages/prog/html/ajax/confirm.html", "")
}
</script>
</head>
<body>
<form  id="search" action="/search" method="get">
<input type=button onclick=logout1() value=logout>
</form >

带有确认代码的示例页面-> div的任何有效html都可以是 confirm.jsp 采取行动...确定吗?

Sample page with confirm code -> any valid html for the div could be confirm.jsp Do action ... Are you sure?

您似乎想显示确认信息-一些UI询问您是否真的要注销?

Looks like you want to show a confirmation - some UI asking do you really want to logout?

最好的方法是创建一个ID为"logoutConfirmSpan"的空跨度,然后单击注销,执行ajax(异步模式-> false,使其发生内联)获取html标记并将其设置为跨度

Best way for that would be to make an empty span with id "logoutConfirmSpan" then on click of logout, do the ajax (async mode -> false so it happens inline) get the html markup and set it to the innerHTML of the span

该html应该有效,以便使UI出现并真正注销表单.

The html should be valid such that it makes the UI appear and the form to really logout.

请参见用Ajax响应替换div的内部HTML 告诉您如何在jQuery中使用ajax

See Replace inner HTML of a div with Ajax response tells you how to use ajax with jQuery

简单示例:

HTML代码段

<span id = 'logoutConfirmSpan'> </span>
<script>
// call ajax, with the response call setHtmlForConfirm
function setHtmlForConfirm(req)
{
    document.getElementById('logoutConfirmSpan').innerHTML = req.responseText;
}
//req.responseText should have the html to show the confirm UI and form to submit yes/ no
</script>

这篇关于如何使用JavaScript在另一个jsp中包含一个jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆