Site icon บริษัท โค๊ดบี จำกัด

วิธีทำ Javascript Share ทั้ง 3 โซเชียล Facebook Google Twitter

<h3>วิธีทำ Javascript Share ทั้ง 3 โซเชียล Facebook Google Twitter<&sol;h3>&NewLine;<p><&sol;br><br &sol;>&NewLine;<strong>วิธีทำ Javascript Share ทั้ง 3 โซเชียล Facebook Google Twitter<&sol;strong><&sol;p>&NewLine;<p>ระบบการแชร์ข้อมูลข่าวสารไปยังโซเชียลต่าง ๆ นั้น ปัจจุบันมีความจำเป็นและสำคัญมาก ๆ โดยเฉพาะอย่างยิ่งมีผลโดยตรงกับการทำ SEO ให้กับเว็บไซต์ จะเห็นได้ว่าในปัจจุบันเว็บไซต์ไม่ว่าจะพัฒนาจาก CMS เว็บไซต์สำเร็จรูปหรือเว็บไซต์แบบ Customize พัฒนาขึ้นมาใหม่ทั้งหมด จะมี social share plugin เป็นองค์ประกอบสำคัญของเว็บไซต์<&excl;--more--><&sol;p>&NewLine;<p><&sol;br><br &sol;>&NewLine;แชร์โซเชียลหลัก ๆ ที่เรามักจะพบมากที่สุดจะมีอยู่ 3 โซเชียลหลัก ๆ คือ facebook&comma; google&plus;&comma; twittter วันนี้ลองมาดูกันครับว่า เราจะสามารถใช้ javascript ในการทำ social plugin ให้กับเว็บไซต์ของเราได้อย่างไร<br &sol;>&NewLine;<&sol;br><&sol;p>&NewLine;<h4>วิธีทำ Javascript Facebook Share<&sol;h4>&NewLine;<p>สำหรับโซเชียลหลักอย่าง facebook โดยปกติแล้วเวลาที่เราแชร์หน้าเว็บไซต์ลงบน facebook timeline ระบบจะนำข้อมูลจาก meta tag open graph ซึ่งจะอยู่ในส่วน head ของเว็บไซต์ เช่น og&colon;title&comma; og&colon;image&comma; og&colon;description เป็นต้น แต่ถ้าในกรณีที่ไม่มี meta tag นี้อยู่ ระบบจะไปดึงข้อมูลจาก meta tag ปกติของเว็บไซต์มาแสดงแทน ตัวอย่าง og meta tag ของเฟสบุ้ค<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;meta property&equals;"og&colon;image" content&equals;"codebee&period;png"&gt&semi; &NewLine;&lt&semi;meta property&equals;"og&colon;title" content&equals;"บริษัท โค๊ดบี จำกัด"&gt&semi; &NewLine;&lt&semi;meta property&equals;"og&colon;type" content&equals;"website"&gt&semi; &NewLine;&lt&semi;meta property&equals;"og&colon;url" content&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th"&gt&semi; &NewLine;&lt&semi;meta property&equals;"og&colon;site&lowbar;name" content&equals;"codebee"&gt&semi; &NewLine;&lt&semi;meta property&equals;"og&colon;description" content&equals;"พัฒนาเว็บไซต์และโมบายแอพพลิเคชั่น"&gt&semi;<&sol;pre>&NewLine;<p>สำหรับการแชร์ข้อมูลหน้าเว็บไซต์เราสามารถแชร์ได้ 2 แบบด้วยกันคือ<&sol;p>&NewLine;<ul>&NewLine;<li>1&period; แชร์ผ่าน Facebook URL Share ด้วยการส่งตัวแปรแบบ Query String ตัวอย่างเช่น&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " > &NewLine;<amp-facebook data-href&equals;"https&colon;&sol;&sol;www&period;facebook&period;com&sol;sharer&period;php&quest;u&equals;url" layout&equals;"responsive" width&equals;"600" height&equals;"400"><button overflow type&equals;"button">See more<&sol;button><&sol;amp-facebook> &NewLine;&amp&semi;caption&equals;caption &NewLine;&amp&semi;description&equals;description &NewLine;&amp&semi;picture&equals;'picture<&sol;pre>&NewLine;<&sol;li>&NewLine;<li>2&period; แชร์ผ่าน Facebook Javascript API ด้วย FB&period;ui ตัวอย่างเช่น&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >FB&period;ui&lpar;&lbrace; &NewLine;&Tab;method&colon; 'feed'&comma; &NewLine;&Tab;name&colon; 'บริษัท โค๊ดบี จำกัด'&comma; &NewLine;&Tab;link&colon; 'https&colon;&sol;&sol;www&period;codebee&period;co&period;th'&comma; &NewLine;&Tab;picture&colon; 'https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;logo&period;png'&comma; &NewLine;&Tab;caption&colon; 'บริษัทรับทำเว็บไซต์และโมบายแอพพลิเคชั่น'&comma; &NewLine;&Tab;description&colon; 'บริษัทรับทำเว็บไซต์และโมบายแอพพลิเคชั่น' &NewLine;&rcub;&rpar;&semi;<&sol;pre>&NewLine;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>วิธีทำ Javascript Google&plus; Share<&sol;h4>&NewLine;<p>สำหรับการแชร์บน Google&plus; นั้น ข้อมูลที่ Google&plus; จะนำไปใช้งานนั้น เริ่มต้นจะดึงมาจากข้อมูลที่เป็นมาตรฐาน schema markup แต่ถ้าไม่มีข้อมูลอยู่ในเว็บไซต์ ก็จะไปดึงข้อมูลมาตรฐาน html คือ meta tag title&comma; image&comma; description เหมือนกับ facebook ตัวอย่าง schema syntag เช่น<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;meta itemprop&equals;"name" content&equals;"บริษัท โค๊ดบี จำกัด"&gt&semi; &NewLine;&lt&semi;meta itemprop&equals;"description" content&equals;"รับทำเว็บไซต์และโมบายแอพพลิเคชั่น"&gt&semi; &NewLine;&lt&semi;meta itemprop&equals;"image" content&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;logo&period;png"&gt&semi;<&sol;pre>&NewLine;<p>เราสามารถแชร์ข้อมูลผ่าน Google&plus; URL Share ได้ดังนี้ครับ<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " > &NewLine;https&colon;&sol;&sol;plus&period;google&period;com&sol;share&quest;url&equals;url&amp&semi;text&equals;title<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<h4>วิธีทำ Javascript Twitter Share<&sol;h4>&NewLine;<p>สำหรับการแชร์ผ่าน Twitter เบื้องต้นข้อมูลที่นำมาแสดงจะถูกดึงมาจาก meta tag twitter แต่ถ้าไม่มีข้อมูลอยู่ก็จะใช้ข้อมูลในลักษณะเดียวกับ facebook และ google&plus; คือใช้ข้อมูล html meta tag มาแสดงแทน ตัวอย่าง twitter meta tag <&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;meta name&equals;"twitter&colon;title" content&equals;"บริษัท โค๊ดบี จำกัด"&gt&semi; &NewLine;&lt&semi;meta name&equals;"twitter&colon;description" content&equals;"รับทำเว็บไซต์และโมบายแอพพลิเคชั่น"&gt&semi; &NewLine;&lt&semi;meta name&equals;"twitter&colon;creator" content&equals;"&commat;codebee"&gt&semi; &NewLine;&lt&semi;meta name&equals;"twitter&colon;image" content&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;logo&period;png"&gt&semi;<&sol;pre>&NewLine;<p>และสามารถแชร์ข้อมูลหรือทวีตหน้าเว็บไซต์ด้วย Twitter URL Share ตัวอย่างเช่น<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >http&colon;&sol;&sol;www&period;twitter&period;com&sol;share&quest;url&equals;url&amp&semi;text&equals;title<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;<p><strong>การแชร์ข้อมูลทั้ง 3 โซเชียลสามารถเขียนให้ทำงานในลักษณะเดียวกันด้วย Javascript ได้ดังนี้ครับ<&sol;strong><&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " > &NewLine;var winWidth &equals; 600&semi; &NewLine;var winHeight &equals; 600&semi; &NewLine;var title &equals; 'หัวข้อเว็บไซต์'&semi; &NewLine;var caption &equals; 'รายละเอียดโดยย่อ'&semi; &NewLine;var description &equals; 'รายละเอียดเต็ม'&semi; &NewLine;var url&lowbar;image &equals; 'รูปที่ต้องการให้แสดงตอนแชร์'&semi; &NewLine; &NewLine;function shareFacebook&lpar;&rpar;&lbrace; &NewLine;&Tab;var url &equals; 'https&colon;&sol;&sol;www&period;facebook&period;com&sol;sharer&period;php&quest;u&equals;'&plus;web&plus;'&amp&semi;caption&equals;'&plus;slogan&plus;'&amp&semi;description&equals;'&plus;description&plus;'&amp&semi;picture&equals;'&plus;url&lowbar;image&semi; &NewLine;&Tab;window&period;open&lpar;url&comma;''&comma;'menubar&equals;no&comma;toolbar&equals;no&comma;resizable&equals;yes&comma;scrollbars&equals;yes&comma;height&equals;'&plus;winWidth&plus;'&comma;width&equals;'&plus;winHeight&rpar;&semi; &NewLine;&rcub; &NewLine;function shareTwitter&lpar;&rpar;&lbrace; &NewLine;&Tab;var url &equals; 'http&colon;&sol;&sol;www&period;twitter&period;com&sol;share&quest;url&equals;'&plus;web&plus;'&amp&semi;text&equals;'&plus;title&semi; &NewLine;&Tab;window&period;open&lpar;url&comma;''&comma;'menubar&equals;no&comma;toolbar&equals;no&comma;resizable&equals;yes&comma;scrollbars&equals;yes&comma;height&equals;'&plus;winWidth&plus;'&comma;width&equals;'&plus;winHeight&rpar;&semi; &NewLine;&rcub; &NewLine;function shareGoogle&lpar;&rpar;&lbrace; &NewLine;&Tab;var url &equals; 'https&colon;&sol;&sol;plus&period;google&period;com&sol;share&quest;url&equals;'&plus;web&plus;'&amp&semi;text&equals;'&plus;title&semi; &NewLine;&Tab;window&period;open&lpar;url&comma;''&comma;'menubar&equals;no&comma;toolbar&equals;no&comma;resizable&equals;yes&comma;scrollbars&equals;yes&comma;height&equals;'&plus;winWidth&plus;'&comma;width&equals;'&plus;winHeight&rpar;&semi; &NewLine;&rcub;<&sol;pre>&NewLine;<p>สามารถใช้คำสั่ง onClick เพื้อแชร์ข้อมูลหน้าเว็บไซต์นั้น ๆ ได้ทันที ตัวอย่างเช่น<&sol;p>&NewLine;<pre class&equals;"lang&colon;default decode&colon;true " >&lt&semi;a onClick&equals;"shareFacebook&lpar;&rpar;&semi;"&gt&semi;Facebook Share&lt&semi;&sol;a&gt&semi; &NewLine;&lt&semi;a onClick&equals;"shareTwitter&lpar;&rpar;&semi;"&gt&semi;Twitter Share&lt&semi;&sol;a&gt&semi; &NewLine;&lt&semi;a onClick&equals;"shareGoogle&lpar;&rpar;&semi;"&gt&semi;Google&plus; Share&lt&semi;&sol;a&gt&semi;<&sol;pre>&NewLine;<p><&sol;br><&sol;p>&NewLine;

Exit mobile version