js修改iframe框架的src路徑及js獲取URL的get參數中文亂碼的問題

中文亂碼問題主要是使用encodeURI和decodeURI解決 index頁面:javascript

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Cache-Control" content="no-Cache" />

    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport" id="viewport" />
    <title>iframe框架</title>
    <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>

    <link href="css/public.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="css/tuanche.css">
</head>

<body >
    
	<iframe src="" width="200" height="500" id="myIframe"> 
		
	</iframe> 
	
	<script type="text/javascript">
	       
		var urlValue = encodeURI(encodeURI("iframePage.jsp?name=何洪波&phone=13243764322&city=深圳市&channel=FF001&appsku=h5_jrtt"));// 這裏必定要轉兩次碼,否則解析出來的依然是亂碼

		$('#myIframe').attr('src',urlValue);
			 
	</script>
    
</body>
</html>

iframe頁面:css

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!doctype html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Cache-Control" content="no-Cache" />

    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no" name="viewport" id="viewport" />
    <title>iframe</title>
    <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        // 獲取get參數
		String.prototype.getQueryString = function(name){
		  var reg = new RegExp("(^|&|\\?)"+ name +"=([^&]*)(&|$)"), r;
		  if (r=this.match(reg)) return unescape(r[2]); return null;
		};
		
		// 使用
		var name = location.search.getQueryString("name");
		var phone = location.search.getQueryString("phone");
		var city = location.search.getQueryString("city");
		var channel = location.search.getQueryString("channel");
		var appsku = location.search.getQueryString("appsku");
		
		alert(decodeURI(name));
		alert(phone);
		alert(decodeURI(city));
		alert(channel);
		alert(appsku);
		
    </script>
    
    <link href="css/public.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="css/tuanche.css">
</head>

<body>
    
    <div>wo s hehognbo</div>
</body>
</html>
相關文章
相關標籤/搜索