2016-06-18 10 views
-1

これはコードですが、これは非常に単純で、私が達成したいもののプロトタイプのようです。私は単に画面のトップ10%にボタンや何かをつけて、これらの行に沿ってiframe内のランダムなリンクを開き、残りの画面を占めるようにしたいだけです。しかし、これをターゲットiframeで開くのに問題があります。だから私はそれをあまりにもあまりにも乱し始めた前にコードがあります。私はjavascriptをまったく知らないので、私はこれをまったく手に入れました。以下のiframeで開きたい特定のスクリプトを持っています

<body> 

<script> 
<!-- 
/* 
Random link button- By JavaScript Kit (http://javascriptkit.com) 
Over 300+ free scripts! 
This credit MUST stay intact for use 
*/ 

//specify random links below. You can have as many as you want 
var randomlinks=new Array() 

randomlinks[0]="http://houvv.deviantart.com/art/Water-Spirit-498202921" 
randomlinks[1]="https://www.artstation.com/artwork/b08Jr" 
randomlinks[2]="http://leekent.deviantart.com/art/Night-Stalker-505269510" 
randomlinks[3]="http://joshtffx.deviantart.com/art/Firekeeper-Dark-Souls-3-609065320" 

function randomlink(){ 
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)] 
} 
//--> 
</script> 
<form method="post"> 
<center><p><input type="button" name="B1" value="Dreamquest" onclick="randomlink()"></p> </form></center> 

<!--Uncomment below to use a regular text link instead 
<a href="javascript:randomlink()">Random Link</a> 
--> 
<p> 
<iframe src="stumble.html" frameborder="0" noresize="noresize" name="iframe" style="position:relative; background:transparent; width:100%;height:100%;top:40px;padding:0;" /> 
</body> 
+0

ようこそ! [ツアー]を見て回り、[ヘルプ]、特に[*どのように良い質問をしますか?*](/助け/やり方) –

答えて

0

代わりに配列を作成し、JavaScriptで動的なものを定義することができるので、その要素を定義する

var randomlinks = ['http://houvv.deviantart.com/art/Water-Spirit-498202921','https://www.artstation.com/artwork/b08Jr','http://leekent.deviantart.com/art/Night-Stalker-505269510','http://joshtffx.deviantart.com/art/Firekeeper-Dark-Souls-3-609065320']; 

を行うことができます。

あなたのフォーム要素の後にあなたの中心要素を終えたので、エラーが

<form method="post"><center><p><input type="button" name="B1" >value="Dreamquest" onclick="randomlink()"></p> </form></center> 

が、これは正しいコードになり

<form method="post"><center><p><input type="button" name="B1" >value="Dreamquest" onclick="randomlink()"></p></center> </form> 

、これらのラインにここにもあります。

しかし、とにかく、あなたのコードはエラーがあってもうまくいくように見えるので、問題は見えません。

編集: は、私は少しhttps://jsfiddle.net/xxmq68e9/

function randomlink(){ 
var iframe = document.getElementById('iframe'); 
iframe.setAttribute('src','https://www.artstation.com/artwork/b08Jr'); 
} 

が動作しているようにコードを微調整し、私がエラー

を得続けるため、ウェブサイトがiframe内に自分のウェブページの読み込みを禁止しているようです

'X-Frame-Options'が 'SAMEORIGIN'に設定されているため、URLをフレームに表示することを拒否しました。スタックオーバーフローへ

How to set 'X-Frame-Options' on iframe?

+0

ああ、問題があります同じタブの全く新しいページではなく、 "iframe"という名前のiframeで開くようにしています。私はiframeを "stumble"に改名し、Firefoxの問題の場合に "id"を追加しようとしました。そして、target = "stumble"をフォーム要素に入れました。この点ではうまくいきません。 – resriel

関連する問題