1. 程式人生 > >js給iframe動態設定src和引數引數傳遞問題

js給iframe動態設定src和引數引數傳遞問題

最近開始接觸到一個挺老專案,其中涉及到了一些jsp程式碼編寫,

其中遇到一個問題比較麻煩,就是在jsp頁面中iframesrc屬性動態修改的問題

頁面:hello.jsp,table.jsp

頁面上有三個標籤:text,button,iframe

實現的功能是:點選button標籤,將text標籤內容作為src 的請求引數傳遞給服務端的jsp頁面,在hello.jsp中動態顯示子視窗表格內容

下面直接貼出程式碼:

hello.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
    
</head>
<!-- <script src="/js/jquery.min.js"></script> -->
<script type="text/javascript">
 function clickButton(){
  var text = document.getElementById("name1").value;
  var frame = document.getElementById('frame1');
  frame.src="table.jsp?text="+text;
}
</script>


<body>
    Hello ${name}
    <br/><br/><br/><br/>
<input id="name1" type="text"></input>
<input id="buttton1" type="button" onclick="clickButton()" value="Click me"/>

<iframe src="table.jsp" id="frame1"
frameborder="0" marginheight="0" marginwidth="0" height="700" width="100%"></iframe>
</body>
</html>

table.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
<!DOCTYPE html>

<%@ page import="springbootjsp.springbootjspweb.config.SpringManager"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="springbootjsp.springbootjspweb.service.HelloService"%>

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>HELLO</title>
</head>
<body>
<% 
	String text = request.getParameter("text");
	//HelloService helloService = SpringManager.getApplicationContext().getBean(HelloService.class);
%>
   
    transport text from parent window is : <%=text %>
</body>
</html>
希望對大家有用吧,jsp(servlet) 從request中獲取就可以了

如果希望使用post方法,要結合表單(form)的使用,注意form的target屬性就ok了