2016-05-10 4 views
0

作成した変数で現在のiframeソースを設定しようとしていますが、動作していないようです。 JavaScriptの構文が間違っていると思った。iframeソースに変数を設定するにはどうすればよいですか?

私のスクリプトは次のとおりです。

  <script type="text/javascript"> 
       $(document).ready(function(){ 
       var iframeAttributes = window.location.search; 
       var iframeForm = 'www.test.com/form.html'; 
       var iframeURL = iframeForm + iframeAttributes; 
       $('.testframe').attr('src', iframeURL); 
       }); 
      </script> 

マイIFrameのコードは次のとおりです。

  <iframe id="testframe" src="" width="600" height="586" frameBorder="0">></iframe> 
+2

ブラウザの開発者ツールの要素インスペクタで見ましたか? srcは設定されていますか? [ネットワーク]タブでURLのリクエストが表示されますか?それはあなたが期待しているURLですか?あなたはあなたが期待している反応を得ていますか? – Quentin

答えて

1

あなたのセレクタは間違っている:

$('.testframe').attr('src', iframeURL);

これはクラスを持つ要素を見つけようとしますtestframeあなたのHTMLにはIDが設定されていますが、クラスには設定されていません。あなたが任意のiframeのあなたにそれを適用することができるように

<script type="text/javascript"> 
$(document).ready(function(){ 
    var iframeAttributes = window.location.search; 
    var iframeForm = 'http://www.test.com/form.html'; 
    var iframeURL = iframeForm + iframeAttributes; 
    $('#testframe').attr('src', iframeURL); 
}); 
</script> 
1

は、プロトコルのhttpを含める:

これを使用して、ID testIframeを持つ要素を選択します欲しいもの:

function changeIframeSrc(id, source){ 
     $(id).attr('src', source); 
} 

私たちの例年齢:

changeIframeSrc("#iframe_tab", "http://google.com"); //Notice you can also pass a class into the function instead of an id 
+0

先頭に//を使用すると、プロトコルなしで動作する場合もあります。 – yezzz

0

使用機能:またはhttps:

$('#testframe').attr('src', iframeURL);

関連する問題