Diễn đàn Trái Tim Yên Bái 2024

 Quên mật khẩu
 Đăng ký
Xem: 4877|Trả lời: 0
Thu gọn cột thông tin

Tự điều chỉnh độ cao cho thẻ IFRAME

[Lấy địa chỉ]
  • Tâm trạng hàng ngày
    Hạnh phúc
    2-7-2025 09:43 PM
  • Đã viết: 13 ngày

    [LV.3]Spam 3

    Đăng lúc 23-4-2008 11:31 PM | Xem tất |Chế độ đọc

    Mời bạn đăng ký để giao lưu kết bạn nhé <3

    Bạn phải đăng nhập để xem được nội dung, nếu bạn chưa có tài khoản? hãy Đăng ký

    x
    Có bao giờ bạn dùng thẻ IFRAME để nhúng một trang khác vào trang hiện tại của mình chưa? Chắc chắn bạn sẽ dùng đọan mã tương tự như sau:
    [html]<iframe src="index.html" width="100%" height="500"></iframe>[/html]
    Đó là phần mã nguồn, thế trong trình duyệt bạn sẽ trông thấy trang này được xử lý ra sao. Phần chiều cao của trang nhúng trong thẻ IFRAME thường không đủ chiều cao của trang và thanh cuộn sẽ xuất hiện, nói nôm na là trang web nằm trong thẻ IFRAME có thể quá thấp, hoặc quá cao so với trang chính.
    b.Vậy điều kiện đặt ra sẽ là làm sao cho trang nằm trong thẻ IFRAME tự động thay đổi chiều cao Height của chính nó sao cho thanh cuộn không xuất hiện bên trong và trải vừa với cả trang.
    Trước đây có nhiều bạn hỏi Shacker làm sao, nhưng lúc đó chưa có thời gian để hướng dẫn, nên bây giờ trong bài viết này Shacker sẽ chỉ dẫn lại tường bước kỹ càng để bạn có thể tự áp dụng phương thức này vào trang web của bạn.
    II - Lợi ích dùng thẻ IFRAME + SH IFRAME HEIGHT AUTO RESIZE
    a.Thẻ IFRAME
    + Cho phép lồng trang khác vào website
    + Có thể kết hợp nâng cao để khi click lên liên kết, cả trang sẽ không thay đổi và chỉ nội dung ở trang IFRAME chính thay đổi
    + Tiết kiệm bandwidth của máy chủ, tăng tốc độ duyệt trang tối đa cho người tham quan và nâng cao tính chuyên nghiệp cho trang web
    + Kỹ thuật AJAX ngày càng được sử dụng nhiều vào các trang web, lợi ích của nó mà mọi người mong đợi là một ứng dụng web có tính tương tác cao và tải nhanh chóng. Từng bước làm quen với nó bạn cần những bài học cơ bản nhất, và việc sử dụng tính năng của thẻ IFRAME cho bạn cái nhìn chung bao quát trước khi tìm hiểu AJAX họat động như thế nào và làm sao để tích hợp được nó vào website.
    Mẫu
    http://hacking.all4elva.com
    b.Đọan mã SH IFRAME HEIGHT AUTO RESIZE
    + Thay đổi kích thước độ cao thẻ IFRAME để trải dài tòan bộ trang và không xuất hiện thanh cuộn làm mất thẩm mỹ trang web
    III - Một số thuộc tính cần biết của thẻ IFRAME
    a.align="giá trị" .:. bao gồm middle,left,right. Ví dụ align="middle"
    b.id="tên bất kỳ" .:. gán giá trị ID nhận dạng cho thẻ IFRAME, dùng để nhận lệnh khi một đối tượng khác gọi đến. Ví dụ id="Shacker"
    c.onLoad="Lệnh thực thi" .:. Lệnh sẽ chạy khi trang được tải.
    d.src="link liên kết một trang khác" .:. Nhúng một website, một trang web khác vào trang hiện tại. Ví dụ src="http://www.silveryhat.com/"
    e.name="tên bất kỳ" .:. gán giá tên cho thẻ IFRAME, dùng khi một liên kết được nhấn lên và tải vào thẻ IFRAME mạng tên này. Ví dụ name="noidung"
    f.width="giá trị số" .:. Điền vào số, hoặc phần trăm để chỉnh độ dài của trang trong thẻ IFRAME. Ví dụ width="100%"
    g.frameborder="0" .:. Hiện đường viền bao bọc trang hoặc không. Giá trị thường dùng là 1 (yes) hoặc 0 (no)
    h.scrolling="auto" .:. Hiện thanh cuộn của IFRAME hoặc không. Bao gồm 3 giá trị yes/no/auto
    IV - Mã SH IFRAME HEIGHT AUTO RESIZE
    Chèn đọan mã sau vào sau thẻ <head> trong trang web của bạn

    1. <script language="JavaScript">
    2. function calcHeight()
    3. {
    4. //SH Iframe Height Auto Resize v.01
    5. //Used to resize the height of IFRAME to fit the content without any scrollbar.
    6. //Coded by Silvery Hat Hacker 2006
    7. var dodai_trang = document.getElementById('Shacker').contentWindow.document.body.scrollHeight;
    8. document.getElementById('Shacker').height=dodai_trang;
    9. }
    10. </script>
    Sao chép mã

    Bạn có để ý thấy chữ Shacker trong đọan mã trên?
    + Ý nghĩa của nó là cho giá trị dodai_trang là độ dài của cả trang được nhúng vào thẻ IFRAME có ID được gán là Shacker (xem lại phần III - b )
    + Ở dòng thứ 2, đọan script sẽ gọi thẻ IFRAME có ID là Shacker lần nữa, sau đó ra lệnh cho độ dài của nó thay đổi khớp với độ dài của trang được nhúng vào. (Vậy là giải quyết được yêu cầu ở phần I - cool.gif
    Tuy nhưng làm sao để nó tự động thực hiện điều đó mỗi khi trang nhúng thay đổi? Nếu bạn chú ý ở phần III - c , bạn sẽ có một khái niệm cho câu trả lời của mình.
    IV - Điều chỉnh thẻ IFRAME
    Ở thẻ IFRAME trong bước một, bạn sẽ thêm vào 2 giá trị quan trọng, và xóa đi thuộc tính height, vì ta không cần nó nữa.
    1. <iframe src="index.html" width="100%" id="Shacker" onLoad="calcHeight();"></iframe>
    Sao chép mã


    + Thuộc tính ID được gán để đọan mã SH Iframe Height Auto Resize gọi khi cần đến, thẻ onLoad sẽ gọi lại chức năng calcHeight() trong đọan script khi trang nhúng trong thẻ IFRAME thay đổi, khi đó độ dài thẻ IFRAME sẽ tự thay đổi cho khớp
    + Bạn có thể thêm một số thuộc tính khác vào thẻ IFRAME tùy ý.

    1. <iframe src="index.html" width="100%" id="Shacker" onLoad="calcHeight();" name="content" frameborder="0" scrolling="auto"></iframe>
    Sao chép mã


    V - Một ví dụ mở rộng
    Bạn copy đọan mã sau vào Notepad và Save dưới dạng HTML, cùng chung thư mục với hai tập tin index.html và index2.html

    1. <html>
    2. <head>
    3. <script language="JavaScript">
    4. function calcHeight()
    5. {
    6. //SH Iframe Height Auto Resize v.01
    7. //Used to resize the height of IFRAME to fit the content without any scrollbar.
    8. //Coded by Silvery Hat Hacker 2006
    9.   var dodai_trang = document.getElementById('Shacker').contentWindow.document.body.scrollHeight;
    10.   document.getElementById('Shacker').height=dodai_trang;
    11. }
    12. </script>
    13. </head>
    14. <body>
    15. <a href="index.html" target="content">Google</a> <a href="index2.html" target="content">Yahoo Mail</a>
    16. <iframe src="index.html" width="100%" id="Shacker" onLoad="calcHeight();" name="content" frameborder="0" scrolling="no"></iframe>
    17. </body>
    18. </html>
    Sao chép mã


    Mã nguồn tập tin index2.html


    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    2. <html>
    3. <head>
    4. <title>ImageShack® - Hosting</title>
    5. <link rel="stylesheet" href="[URL]http://www.imageshack.us/img/style-def.css[/URL]" type="text/css">
    6. <link rel="shortcut icon" href="[URL]http://www.imageshack.us/favicon.ico[/URL]" type="image/x-icon" />

    7.    <script language='javascript' src='http://127.0.0.1:1026/js.cgi?pca&r=4449'></script>
    8. <script type="text/javascript">
    9. function highlight(field) {
    10.         field.focus();
    11.         field.select();
    12. }
    13. var url = "[URL]http://imageshack.us/[/URL]";
    14. var title = "ImageShack.us® - Image Hosting";
    15. function favorites(){
    16. if(document.all)
    17. window.external.AddFavorite(url,title)
    18. }
    19. function disableme (what) {
    20. what = document.getElementById(what);
    21. what.disabled = true;
    22. what.value="uploading...";
    23. }
    24. var buttonname = '"Browse..."';
    25. function showoptions(what) {
    26. var ext = what.value.substr(what.value.length - 3,3).toLowerCase();
    27. switch (ext) {
    28. case 'jpg':
    29. case 'peg':
    30. case 'png':
    31. case 'gif':
    32. case 'bmp':
    33. case 'tif':
    34. case 'iff':
    35. document.getElementById('resizeoptions').style.display='';
    36. document.getElementById('filetypeerror').style.display='none';
    37. document.getElementById('butan').disabled=false;
    38. document.getElementById('butan').value='host it!';
    39. break;
    40. case 'swf':
    41. document.getElementById('resizeoptions').style.display='none';
    42. document.getElementById('filetypeerror').style.display='none';
    43. document.getElementById('butan').disabled=false;
    44. document.getElementById('butan').value='host it!';
    45. break;
    46. case '':
    47. document.getElementById('butan').disabled=true;
    48. document.getElementById('butan').value=buttonname;
    49. default:
    50. document.getElementById('resizeoptions').style.display='none';
    51. document.getElementById('filetypeerror').style.display='';
    52. document.getElementById('butan').disabled=true;
    53. document.getElementById('butan').value='bad file type';
    54. break;
    55. }
    56. }
    57. function toggleuploadmode(file) {
    58. if (file) {
    59.   document.getElementById('upfile').style.display='block';
    60.   document.getElementById('upurl').style.display='none';
    61.   document.getElementById('upform').action='http://www.imageshack.us/index.php';
    62. } else {
    63.   document.getElementById('upfile').style.display='none';
    64.   document.getElementById('upurl').style.display='block';
    65.   document.getElementById('upform').action='http://www.imageshack.us/transload.php';
    66. }
    67. }
    68. function focusfield(fl) {
    69. if (fl.value=="paste image url here") {
    70.   fl.value='';
    71.   fl.style.color='black';
    72. }
    73. }
    74. -->
    75. </script>
    76. </head>
    77. <body bgcolor="#F7F7F7">
    78. <table width="760" cellspacing="0" cellpadding="0" border="0" align="center"><tr><td><a href="<A href="http://www.imageshack.us/"><img">http://www.imageshack.us/"><img alt="ImageShack® Logo" src="[URL]http://imageshack.us/img/imageshack.png[/URL]" title="" /></a></td><td valign="bottom" align="left">
    79. <b><a href="<A href="http://www.imageshack.us/">ImageShack®</a">http://www.imageshack.us/">ImageShack®</a>
    80. | <a href="<A href="http://www.imageshack.us/slideshow/">Slideshows</a">http://www.imageshack.us/slideshow/">Slideshows</a> | <a href="<A href="http://reg.imageshack.us/content.php?page=features">Tools</a">http://reg.imageshack.us/content.php?page=features">Tools</a>
    81. | <a href="<A href="http://reg.imageshack.us/content.php?page=faq">Common">http://reg.imageshack.us/content.php?page=faq">Common Questions</a>
    82. | <a href="<A href="http://reg.imageshack.us/content.php?page=rules">Terms">http://reg.imageshack.us/content.php?page=rules">Terms of Service</a>
    83. <!-- | <a href="<A href="http://toolbar.imageshack.us">Download">http://toolbar.imageshack.us">Download Toolbar</a> !-->
    84. | <a href="<A href="http://reg.imageshack.us/content.php?page=register">My">http://reg.imageshack.us/content.php?page=register">My Images</a> | <a href="<A href="http://reg.imageshack.us/content.php?page=developer">Dev">http://reg.imageshack.us/content.php?page=developer">Dev Center</a>
    85. </b>
    86. </td></tr></table>
    87. <script src="[URL]http://www.google-analytics.com/urchin.js[/URL]" type="text/javascript">
    88. </script>
    89. <script type="text/javascript">
    90. _uacct = "UA-277659-1";
    91. urchinTracker();
    92. </script>
    93. <table cellpadding="5" width="760" border="1" bordercolor="#CCCCCC" class="table_decoration" align="center"><tr><td valign="top"><!-- BEGIN STANDARD TAG - 300 x 250 - imageshack: Medium Rectangle - DO NOT MODIFY -->
    94. <script TYPE="text/javascript" SRC="<A href="http://content.yieldmanager.com/rmtag3.js"></SCRIPT">http://content.yieldmanager.com/rmtag3.js"></SCRIPT>
    95. <script language="JavaScript">
    96. var rm_host = "[URL]http://ad.yieldmanager.com[/URL]";
    97. var rm_section_id = 62324;
    98. var rm_iframe_tags = 1;
    99. rmShowAd("300x250");
    100. </SCRIPT>
    101. <!-- END TAG -->
    102. </td><td valign="top"><p>ImageShack's mission is to provide an easy-to-use image hosting service.</p><p>Read the <a href="<A href="http://reg.imageshack.us/content.php?page=rules"><b>Terms">http://reg.imageshack.us/content.php?page=rules"><b>Terms of Service</b></a> and Bookmark <a rel="sidebar" title="ImageShack® - Sidebar" href="[URL]http://imageshack.us/index4.php[/URL]" onclick="window.external.AddFavorite(location.href,document.title);window.sidebar.addPanel(document.title,location.href,'');"><b>ImageShack's Sidebar</b></a>.</p><p><b><a href="<A href="http://reg.imageshack.us/content.php?page=faq">New">http://reg.imageshack.us/content.php?page=faq">New to ImageShack</a></b>? Start an <a href="<A href="http://imageshack.us/slideshow/"><b>Image">http://imageshack.us/slideshow/"><b>Image Slideshow</b></a>, or upload now:</p><br />
    103. <form method="post" action="[URL]http://www.imageshack.us/[/URL]" enctype="multipart/form-data" onsubmit="disableme('butan')" id="upform">
    104. <table cellspacing="0" cellpadding="0" border="0"><tr>
    105. <td><p style="font-size: 11px;">upload: <input type="radio" name="uploadtype" onclick="toggleuploadmode(true);" checked>
    106. file <input type="radio" name="uploadtype" onclick="toggleuploadmode(false);"> url</p></td>
    107. <td width="2">&nbsp;</td>
    108. <td><div id="upfile"><input type="file" name="fileupload" size="30" onchange="showoptions(this)" id="fileupload"></div>
    109. <div id="upurl" style="display: none"><input type="text" id="fileupload" value="paste image url here" style="color: #888;"
    110. onfocus="focusfield(this)" name="url" size="30"></div></td>
    111. </tr></table>

    112. <input type="hidden" name="MAX_FILE_SIZE" value="3145728">
    113. <input type="hidden" name="refer" value="">
    114. <input type="hidden" name="brand" value="">
    115. <div id="resizeoptions">
    116.   <input type="checkbox" name="optimage" id="optimage" value="1"  onclick="optsize.disabled=!this.checked"> resize image?
    117.   <select name="optsize" id="optsize">
    118.     <option value="100x100" >100x75 (avatar)</option>
    119.     <option value="150x150" >150x112 (thumbnail)</option>
    120.     <option value="320x320" selected>320x240 (for websites and email)</option>
    121.     <option value="640x640" >640x480 (for message boards)</option>
    122.     <option value="800x800" >800x600 (15-inch monitor)</option>
    123.     <option value="1024x1024" >1024x768 (17-inch monitor)</option>
    124.     <option value="1280x1280" >1280x1024 (19-inch monitor)</option>
    125.     <option value="1600x1600" >1600x1200 (21-inch monitor)</option>
    126.     <option value="resample" >optimize without resizing</option>
    127.   </select><br />

    128.   <input type="checkbox" name="rembar" value="1" > remove size/resolution bar from thumbnail?
    129. </div>
    130. <div id="filetypeerror" style="display: none"> <b style="color: red;">Please select only one of the supported file types:</b><br /></div>
    131. <p>allowed: <b>jpg jpeg png gif bmp tif tiff swf</b> < <b>1.5 megabytes</b>.</p>
    132. <input id="butan" style="width:135px" type="submit" value="host it!">&nbsp;&nbsp;&nbsp;&nbsp;Install the free <b><a href="<A href="http://toolbar.imageshack.us/">ImageShack">http://toolbar.imageshack.us/">ImageShack Toolbar</a></b> now!</form></td></tr></table><div align="center" class="links">
    133. <a href="<A href="http://reg.imageshack.us/content.php?page=rules">Copyright</a">http://reg.imageshack.us/content.php?page=rules">Copyright</a> &copy; 2003-2006 <a
    134. href="<A href="http://reg.imageshack.us/content.php?page=aboutus">ImageShack">http://reg.imageshack.us/content.php?page=aboutus">ImageShack Corp</a>. All
    135. rights reserved. <a href="<A href="http://reg.imageshack.us/content.php?page=linkto">Link">http://reg.imageshack.us/content.php?page=linkto">Link to ImageShack</a>
    136. <br />
    137. <a href="<A href="http://reg.imageshack.us/content.php?page=email&amp;q=marketing">Marketing">http://reg.imageshack.us/content.php?page=email&amp;q=marketing">Marketing Opportunities</a> |
    138. <a href="<A href="http://reg.imageshack.us/content.php?page=email&amp;q=abuse">Report">http://reg.imageshack.us/content.php?page=email&amp;q=abuse">Report Abuse or Request Deletion</a> |
    139. <a href="<A href="http://reg.imageshack.us/content.php?page=email&amp;q=customer">Customer">http://reg.imageshack.us/content.php?page=email&amp;q=customer">Customer Service</a>
    140. <br />
    141. <a href="<A href="http://reg.imageshack.us/content.php?page=email">Contact">http://reg.imageshack.us/content.php?page=email">Contact ImageShack</a>
    142. </div>
    143. <table width="385" cellspacing="0" cellpadding="3" border="0" class="table_decoration" align="center"><tr><td>
    144. <p><a href="<A href="http://toolbar.imageshack.us/"><img">http://toolbar.imageshack.us/"><img src="[URL]http://imageshack.us/toolbar_img.png[/URL]" /></a></p>
    145. <p>Install the free <b><a href="<A href="http://toolbar.imageshack.us/">ImageShack">http://toolbar.imageshack.us/">ImageShack Toolbar</a></b> now!</p>
    146. </td></tr></table> <br />

    147. </body>
    148. </html>
    Sao chép mã


    Mã nguồn tập tin index.html


    1. <html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><title>Google</title><style><!--
    2. body,td,a,p,.h{font-family:arial,sans-serif;}
    3. .h{font-size: 20px;}
    4. .q{color:#0000cc;}
    5. -->
    6. </style>
    7. <script>
    8. <!--
    9. function sf(){document.f.q.focus();}
    10. function clk(url,oi,cad,ct,cd,sg){if(document.images){var e = window.encodeURIComponent ? encodeURIComponent : escape;var u="";var oi_param="";var cad_param="";if (url) u="&url="+e(url).replace(/\+/g,"%2B");if (oi) oi_param="&oi="+e(oi);if (cad) cad_param="&cad="+e(cad);new Image().src="/url?sa=T"+oi_param+cad_param+"&ct="+e(ct)+"&cd="+e(cd)+u+"&ei=ByevRLztCbmaaNfJzKMI"+sg;}return true;}
    11. // -->
    12. </script>
    13. </head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onLoad=sf() topmargin=3 marginheight=3><center><table border=0 cellspacing=0 cellpadding=0 width=100%><tr><td align=right nowrap><font size=-1><a href="/url?sa=p&pref=ig&pval=3&q=http://www.google.com/ig%3Fhl%3Den" onmousedown="return clk('/url?sa=p&pref=ig&pval=3&q=http://www.google.com/ig%3Fhl%3Den','promos','hppphnu:en_us','pro','1','')">Personalized Home</a>&nbsp;|&nbsp;<a href="<A href="https://www.google.com/accounts/Login?continue=http://www.google.com/&hl=en">Sign">https://www.google.com/accounts/Login?continue=http://www.google.com/&hl=en">Sign in</a></font></td></tr><tr height=4><td><img alt="" width=1 height=1></td></tr></table><img src="[URL]http://www.google.com/intl/en/images/logo.gif[/URL]" width=276 height=110 alt="Google"><br><br>
    14. <form action=/search name=f><script><!--
    15. function qs(el) {if (window.RegExp && window.encodeURIComponent) {var ue=el.href;var qe=encodeURIComponent(document.f.q.value);if(ue.indexOf("q=")!=-1){el.href=ue.replace(new RegExp("q=[^&$]*"),"q="+qe);}else{el.href=ue+"&q="+qe;}}return 1;}
    16. // -->
    17. </script><table border=0 cellspacing=0 cellpadding=4><tr><td nowrap><font size=-1><b>Web</b>&nbsp;&nbsp;&nbsp;&nbsp;<a id=1a class=q href="/imghp?hl=en&tab=wi" onClick="return qs(this);">Images</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=2a class=q href="[URL]http://groups.google.com/grphp?hl=en&tab=wg[/URL]" onClick="return qs(this);">Groups</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=4a class=q href="[URL]http://news.google.com/nwshp?hl=en&tab=wn[/URL]" onClick="return qs(this);">News</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=5a class=q href="[URL]http://froogle.google.com/frghp?hl=en&tab=wf[/URL]" onClick="return qs(this);">Froogle</a>&nbsp;&nbsp;&nbsp;&nbsp;<a id=7a class=q href="/maphp?hl=en&tab=wl" onClick="return qs(this);">Maps</a>&nbsp;&nbsp;&nbsp;&nbsp;<b><a href="/intl/en/options/" class=q>more&nbsp;&raquo;</a></b></font></td></tr></table><table cellspacing=0 cellpadding=0><tr><td width=25%>&nbsp;</td><td align=center><input type=hidden name=hl value=en><input maxlength=2048 size=55 name=q value="" title="Google Search"><br><input type=submit value="Google Search" name=btnG><input type=submit value="I'm Feeling Lucky" name=btnI></td><td valign=top nowrap width=25%><font size=-2>&nbsp;&nbsp;<a href=/advanced_search?hl=en>Advanced Search</a><br>&nbsp;&nbsp;<a href=/preferences?hl=en>Preferences</a><br>&nbsp;&nbsp;<a href=/language_tools?hl=en>Language Tools</a></font></td></tr></table></form><br><font size=-1>Share pictures with <a href=http://picasa.google.com/pack.html onmousedown="return clk(this.href,'promos','hppwebwinxp:en_us','pro','1','')">Google's photo organizer</a>, part of Google Pack</font><br><br><br><font size=-1><a href="/intl/en/ads/">Advertising&nbsp;Programs</a> - <a href=/services/>Business Solutions</a> - <a href=/intl/en/about.html>About Google</a><span id=hp style="behavior:url(#default#homepage)"></span>
    18. <script>
    19. //<!--
    20. if (!hp.isHomePage('http://www.google.com/')) {document.write("<p><a href="/mgyhp.html" onClick="style.behavior='url(#default#homepage)';isHomePage ('http://www.google.com/');">Make Google Your Homepage!</a>");}
    21. //-->
    22. </script></font><p><font size=-2>&copy;2006 Google</font></p></center></body></html>
    Sao chép mã
    Bạn phải đăng nhập mới được đăng bài Đăng nhập | Đăng ký

    Quy tắc điểm

    Đóng

    Thông báoTrước /1 Sau

    Phòng tối|Lưu trữ|Diễn đàn Yên Bái 2024 Community Guidelines |

    GMT+7, 13-10-2026 02:48 AM , Processed in 0.019271 second(s), 18 queries .

    Powered by Discuz! X3.4

    © 2001-2013 Comsenz Inc.

    Trả lời nhanh Lên trên Trở lại danh sách