บทความ > Javascript

Window popup call function on window opener Turn Back

เมื่อ 2016-09-08 11:07:33

How to call the function on parent window with javascript ?

Main page

<html>
<head>
 
<script>
function openNew(){
      popup_path = 'popup.html;
      var myWindow = window.open(popup_path,'WindowName','Left=100,Top=100,width=400,height=380, resizable=1,scrollars=1, toolbar=1');
      if(!myWindow.opener) {
        myWindow.opener = self; 
        myWindow.focus();
    }
}
 
function returnData(aVal, bVal){
     alert("Title = "+ aVal + " And Description = "+ bVal);
}
</script>
 
</head>
 
<body>
 
<a href="javascript:void(0)" onclick="openNew()">Open window popup</a>
 
</body>
</html>

popup.html

<html>
<head>
 
<script>
function sendData(){
     Title = document.forms[0].title.value;
     Desc = document.forms[0].desc.value;
 
     // return value to parent
     window.opener.returnData(Title,Desc);
 
     window.close(); 
}
</script>
 
</head>
<body>
 
<form>
      <input type="text" name="title" />
      <input type="text" name="desc" />
      <button onclick="sendData()">OK</button>
</form>
 
</body>
</html>
 
 
Turn Back

Automotive ดูทั้งหมด

IT Update ดูทั้งหมด