-1

This is the code, it seems very simple and it's kind of a prototype of something I want to achieve. I simply want the top 10% or so of my screen to have a button or something along these lines that opens a random link in the iframe that takes up the rest of the screen below. But I'm having trouble getting this to open in the target iframe. So here's the code before I began messing with it too much. I don't know javascript at all, so I'm surprised I got this far at all.

<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>
resriel
  • 57
  • 6
  • Welcome to Stack Overflow! Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Jun 18 '16 at 07:12

1 Answers1

0

You could do

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'];

instead of creating an array and defining its elements, since you can define things dynamically in JavaScript.

There is also an error here on these lines

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

since you ended your center element after your form element,

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

this would be the correct code.

But anyway, your code seems to work even with the errors so I don't see the problem.

Edited: I tweaked with your code a little https://jsfiddle.net/xxmq68e9/

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

seems to work, but it seems the websites have disallowed loading of their webpages in an iframe since I keep getting the error

Refused to display URL in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.

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

Community
  • 1
  • 1
Tyler Chong
  • 650
  • 2
  • 12
  • 24
  • Oh, the problem is getting it to open in the iframe named "iframe" and not an entirely new page in the same tab. I tried renaming the iframe to "stumble" and adding an "id" in case it was a Firefox issue, and I put target="stumble" in the form element. It doesn't work, in this regard. – resriel Jun 18 '16 at 07:34