0

Web ページに iFrame があり、他のすべてのページには「ディスカッション」というボタンがあります。ユーザーがこのボタンをクリックすると、iframe ページに移動します。

ユーザーが来たばかりのページを表示する iframe を取得する方法を知りたいですか? つまり、議論ボタンのあるページです。

これは、最終的にユーザーがページを iframe で表示して議論できるようにするためです。

前もって感謝します、ダニエル。

4

1 に答える 1

0

以下は例です。両方のファイルが同じディレクトリにある必要があります。

mainpage.html:

<html>
  <body>
    <script>
      function gotoFramePageNormal() {
        window.open('framepage.html', '_top');
      }
      function gotoFramePageWithVar() {
        window.open('framepage.html?ref='+document.location.href, '_top');
      }
    </script>
    <input type=button value="Discuss" onclick="gotoFramePageNormal()" /> (normal referrer; via HTTP header)<br />
    <br />
    <input type=button value="Discuss" onclick="gotoFramePageWithVar()" /> (with URL variable referrer)<br />
  </body>
</html>

framepage.html:

<html>
  <body>
    <div>
      Source: <span id="srcurl">...</span>
    </div>
    <iframe id="theframe" width=800 height=600></iframe>
    <script>
      (function() {
        if (document.referrer=='') { //no referrer
          //parse URL variables
          var a=document.location.search,b,c;
          while ( (a.length>0) && (a[0]=='?') ) a=a.substring(1,a.length);
          //split variables
          a=a.split('&');
          //check each variable
          for (b=0; b<a.length; b++) {
            //split name and value
            c=a[b].split('=');
            if (c[0].toLowerCase()=='ref') {
              //show source url
              srcurl.innerText=decodeURI(c[1]);
              //set iframe source
              theframe.location.href=decodeURI(c[1]);
              return;
            }
          }
          //show no source url
          srcurl.innerText='none';
        } else { //has referrer
          //show source url
          srcurl.innerText=document.referrer;
          //set iframe source
          theframe.location.href=document.referrer;
        }
      })();
    </script>
  </body>
</html>

mainpage.htmlURL 変数を使用して、ソース ページをフレーム ページに渡します。通常、これは Web ブラウザーによって自動的に行われますが、ユーザーがその機能を無効にできるため、信頼性は十分ではありません。は両方のframepage.html方法を使用して、ソース ページの URL を検出します。

于 2012-07-18T15:45:00.963 に答える