在Html中使用JS变量

时间:2015-05-27 11:18:50

标签: javascript html iframe

我需要访问html iFrame中的javascript变量。下面我将提到到目前为止我已经实现的代码。

<script type="text/javascript">
    var sessionState = '<%=statusCookie%>'
    console.log("======JS sessionState=========="+sessionState);
    </script>
 <iframe id="rpIFrame" src="http://localhost:8080/playground/rpIFrame.jsp?session="+sessionState>
</iframe>

此处控制台日志正确打印​​sessionState值。但是一旦我在iFrame中将其附加到src,sessionState就变为空。请帮我纠正一下。

2 个答案:

答案 0 :(得分:0)

<iframe id="rpIFrame" src="http://localhost:8080/playground/rpIFrame.jsp?session="+sessionState>
</iframe>
<script type="text/javascript">
    var sessionState = '<%=statusCookie%>'
    document.getElementById("rpIFrame").setAttribute(src,"http://localhost:8080/playground/rpIFrame.jsp?session="+sessionState );
</script>

答案 1 :(得分:0)

试试这个:

<script type="text/javascript">
    window.onload=function(){
        var sessionState = '<%=statusCookie%>'
        document.getElementById("rpIFrame").src = "http://localhost:8080/playground/rpIFrame.jsp?session="+sessionState
    }
</script>
<iframe id="rpIFrame"></iframe>