Jan
26
2008

เขียน javascript เล่นกับ popup และ opener

9,546 views

พอดีต้องเขียน javascript สั้นๆ เป็นตัวอย่างไปให้ programmer อีกท่านที่ไม่ถนัด javascript
เลยคิดว่าน่าจะเอามาเล่าให้ฟัง

การทำงานคือ หน้าต่างหลักมี text field อยู่ พอกดปุ่มจะเปิด popup ขึ้นมาโดยจะมี textarea
เพื่อกรอกข้อมูล พอกดปุ่ม submit ค่าที่อยู่ใน textarea จาก popup จะไปอยู่ใน text field
ตัวหน้าต่างหลัก, แปลงจากขึ้นบรรทัดใหม่เป็น : แทน และ popup ปิดตัวเอง ตามรูป


ขออธิบายส่วนที่น่าสนใจแล้วกันนะครับ

ไฟล์ test.html (หน้าต่างหลัก)

 
// function นี้มีไว้เพื่อสร้าง popup ให้อยู่กลางจอเสมอ
function popUpWindow(URL, N, W, H, S) { // name, width, height, scrollbars
var winleft    =    (screen.width - W) / 2;
var winup    =    (screen.height - H) / 2;
winProp        =    'width='+W+',height='+H+',left='+winleft+',top=' +winup+',scrollbars='+S+',resizable' + ',status=yes'
Win            =    window.open(URL, N, winProp)
if (parseInt(navigator.appVersion) >= 4) { Win.window.focus(); }
}
function popUpTextArea() {
// สั่งสร้าง popup จากการกดปุ่มที่หน้าต่างหลัก
popUpWindow('popup_textarea.html','SH1',340,130,'no');
}
 

ไฟล์ popup_textarea.html

 
// เมื่อกดปุ่ม Set Value ที่ popup จะอ่านค่าจาก textarea มา
// และแปลงจาก \n (ขึ้นบรรทัดใหม่) เป็น :
function setContent () {
var strContent = document.frmSelect.tbTextArea.value;
strContent = replaceString(strContent,'\n',':');
// window.opener ก็คือตัวที่เปิดมันขึ้นมานั่นเอง
window.opener.document.frmTest.tbTest.value=strContent;
window.close();
}
// เอาไว้ replace ข้อความใน string ตามต้องการ
function replaceString (strString,strFind,strReplace) {
while (strString.indexOf(strFind)>-1) {
intPosition= strString.indexOf(strFind);
strString = '' + (strString.substring(0, intPosition) + strReplace +
strString.substring((intPosition + strFind.length), strString.length));
}
return strString;
}
 

ลองของจริงโดยการ ดาวน์โหลดไฟล์ที่นี่

 


Written by Pipo in: Developer | Tags: , , , ,

6 Comments »

  • แจ๋วไปเลยครับ ตอนแรกนึกว่าจะมีการ Reload หน้าจอหลัก แต่ว่าไม่มีเลยแฮ๊ะ

    Comment by tatump — February 5, 2008
  • ขอบคุณมากครับ

    Comment by yut — August 11, 2009
  • ขอบคุณค่ะ แต่เรามีปัญหาตรงที่ว่า คำสั่ง window.opener.document.frmTest.tbTest.value=strContent;
    นี้ เราประยุกต์เป็น window.opener.document.getElementById(“msg”)โดยตัวแปร msg คือtextarea_name รันใน IE ได้ ปกติดี แต่รันใน Firefox แล้วมันไม่คืนค่ากลับมา form หลักอ่ะค่ะ check error มันขึ้นมาว่า Error: window.opener.document.getElementById(“msg”) has no properties ควรแก้ไขยังไงดีคะ

    Comment by KatMee — October 5, 2009
  • เฮ่อ….. ปัญหาโลกแตก จุดใต้ตำตอจริงๆๆ เราแก้ปัญหาได้แระ งมโข่งมาอยู่ 3วัน
    ที่มันรันใน firefox ไม่ผ่านเป็นเพราะว่า ที่ textarea ใน form หลัก ที่เราจะส่งค่าจาก popup กลับไปนั้น ไม่ได้กำหนด id จาก
    แก้ไขเป็น
    แค่นี้เองอ่ะ… เฮ่อ เลยมาบอกเล่าประสบการณ์ เผื่อใครเจอปัญหาเหมือนเราแล้ว search google เจอบทความนี้ ให้ลอง check code ดีๆๆ ถ้าเราเขียน code ถูก ยังไงมันก้อต้องรันได้..

    Comment by KatMee — October 5, 2009
  • ขอเสริมนิดนึงครับตอนที่ popup ปิดตัวเอง
    น่าจะให้แสดง(active) opener ด้วยน่ะครับ

    window.opener.focus();

    Comment by amz — October 13, 2009
  • ขอบคุณนะครับ

    Comment by Backer — December 29, 2009

RSS feed for comments on this post. TrackBack URL

Leave a comment

Page 1 of 11

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes