How to pass a parameter from iframe to iframe?

2019-08-21 18:38发布

I have a parent jsp called parent.jsp. This spawns an iframe called iframe1, which in turn frames an iframe called iframe2 .

From parent.jsp :

<iframe id="iframe1" style="display:none;" height="430" 
width="675" 
src="iframe1.jsp?myNumber=<%= bean.getMyNumber() %>" >

here, i get the mynumber in iframe1.jsp using :

String myNumber=request.getParameter("myNumber");

Now i need to pass it again to iframe2.jsp. For that i am calling a javascript method called : createSecondIframe(). Here i am using it like this :

createSecondIframe(myNumber)
{
ifrm.setAttribute("src", "iframe2.jsp?myNO=myNumber");
}

Here the iframe2 is getting created with all the visual content but the myNumber is not getting passed for when i do this :

System.out.println("MyNo::"+request.getParameter("myNO"));

I get, MyNO as null.

EDIT :

According to Bruno's suggestion , this the function calling createSecondIframe() :

function iframe1(myNumber)
{
var parentDynamicDiv = window.parent.document.getElementById('overlay1');
var iframe = window.parent.document.getElementById('container1');

iframe.style.display = 'none';
parentDynamicDiv.style.display = 'none';
createSecondIframe(myNumber);
}

Where i am going wrong? Kindly help .

2条回答
爷、活的狠高调
2楼-- · 2019-08-21 19:30

You are not using the myNO variable but are specifying myNO as a string. Try this

createSecondIframe(myNumber)
{
    ifrm.setAttribute("src", "iframe2.jsp?myNO=" + myNumber);
}
查看更多
叛逆
3楼-- · 2019-08-21 19:44

Browser knows nothing about myNumber which exists in context of servlet. You have to explicitly define it somewhere inside client code.

<script type="text/javascript">
    var myNumber = <%= myNumber %>; 
</script>

createSecondIframe(num)
{
ifrm.setAttribute("src", "iframe2.jsp?myNO="+ num);
}

And later call createSecondIframe with myNumber

createSecondIframe(myNumber)
查看更多
登录 后发表回答