私のウェブサイトでは、Facebookのようなものを自分のウェブサイトに追加したいと思います。
これが私が使用したコードです:
document.write('<iframe id="like-box" src="http://www.facebook.com/plugins/like.php?href='+ like_url +'&layout=button_count&show_faces=true&width=450&action=like&colorscheme=light&height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:60px; height:80px;" allowTransparency="true"></iframe>');
var popupDiv = document.getElementById('like-box');
popupDiv.onclick = function(e){
if (!e) e = window.event;
alert("Like button will be removed");
document.removeChild(popupDiv);
};
like_url
私のウェブサイトのURLを含む文字列はどこにありますか。
ユーザーがiframeをクリックしたときにiframeを削除したいのですが。しかし、上記のコードはまったく機能しません。onclick関数がトリガーされることはありません。正しく動作させる方法はありますか?
私もそのようなツイッターボタンで同じことをしようとしました:
document.write('<div id="like-box" style="left:0; top:0; width: 500px; height: 230px;">');
document.write('<scr'+'ipt type="text/javascript" src="https://apis.google.com/js/plusone.js"></scr'+'ipt>');
document.write('<g:plusone href="' + gplus_url + '"></g:plusone>');
document.write('</div>');
var popupDiv = document.getElementById('like-box');
popupDiv.onclick = function(e){
if (!e) e = window.event;
alert("G+ button will be removed");
document.removeChild(popupDiv);
};
しかし、それも機能しません!ウィンドウのクリックをトリガーしようとしましたが、同じ結果が得られました。
それを解決するのを手伝ってもらえますか?
編集:
私はそれをより良い方法で説明しようとします。メインドキュメントからiframe内またはiframe上でクリックイベントを検出したい。つまり、私は自分のWebサイトにいます。Facebookのようにクリックすると、index.htmlページによってクリックがトリガーされ、数秒後にiframeが削除されます。しかし、そのようなものはFBによって捕らえられました。これが基本的に私がやりたいことです。iframeをクリックすると関数をトリガーする多くのコードを試しましたが、どれも機能しませんでした。例えば:
onload="this.contentWindow.document.onclick=function(){alert(\'test\')}"
まったく動作しません。
編集2:ウィンドウでonclikc関数を使用すると、これは深刻な問題のように見えます(たとえばwindow.onclick = function(e) {...};
、iframeをクリックしてもトリガーされません。何か考えはありますか?