main page
Javascript를 이용해서 다음 페이지에 파라미터 넘기기 위한 방법이다.
<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script> function setChildValue(index) { window.location.href = "subPage.html?index=" + index; } </script> </head> <body> <button onclick="setChildValue(123)">Click me</button> </body> </html>
sub page
방법 1
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { var val = location.href.substr( location.href.lastIndexOf('=') + 1 ); console.log('val : ' + val); }); </script> </head> <body> </body> </html>
방법 2
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript"> function getURLParameter(name) { return decodeURI( (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1] ); } function getURLParameter2(name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null } $(document).ready(function () { console.log('val : ' + getURLParameter('index')); console.log('val : ' + getURLParameter2('index')); } </script> </head> <body> </body> </html>
방법 3
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <script type="text/javascript"> var getParameter = function (param) { var returnValue; var url = location.href; var parameters = (url.slice(url.indexOf('?') + 1, url.length)).split('&'); for (var i = 0; i < parameters.length; i++) { var varName = parameters[i].split('=')[0]; if (varName.toUpperCase() == param.toUpperCase()) { returnValue = parameters[i].split('=')[1]; return decodeURIComponent(returnValue); } } }; $(document).ready(function () { console.log('val : ' + getParameter('index')); } </script> </head> <body> </body> </html>
'Web > JavaScript' 카테고리의 다른 글
[JS] 랜덤 수를 가진 배열 만들기 (0) | 2015.03.18 |
---|---|
[javascript] <input type = "image"> submit 막기 (0) | 2013.08.31 |
[javascript] 텍스트박스 클릭시 문자 사라지기 (0) | 2013.08.28 |
[javascript] 브라우저 체크하기 (2) | 2013.08.24 |