The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Getting a randomized link button to target an iframe
bibliovore
post Aug 5 2024, 07:04 PM
Post #1





Group: Members
Posts: 2
Joined: 5-August 24
Member No.: 29,217



Hello,

I'm trying to make a trivia game. Ideally, I have a main webpage containing a button and, on top of the button, an iframe. Clicking the button sends you to a randomized webpage that would contain a question, without ever having to leave the main webpage.

I'm having two problems with my code so far. (1) The button sends my iframe link to my main webpage (i.e. I click the button and I now have my main webpage, with my main webpage again in the iframe), and (2) the button only seems to work on the first click.

Here's my code, thank you!

CODE
<iframe src="/contact" name="A" width="500" height="250"></iframe>

======

<script>

var sites = [

'/capfriendly',

'/capfriendly',

'/capfriendly'

];

function randomSite() {

var i = parseInt(Math.random() * sites.length);

iframe.src= sites[i];

}

</script>

<a href="#" target="A" onclick="randomSite();">BUTTON</a>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
coothead
post Aug 6 2024, 04:33 AM
Post #2


Advanced Member
****

Group: Members
Posts: 234
Joined: 12-January 23
From: chertsey, a small town 25 miles south west of london, england
Member No.: 28,743



Hi there bibliovore,

and welcome to these forums. IPB Image


Check out the attachment to see a working example.

Attached File  bibliovore.zip ( 2.41k ) Number of downloads: 135




coothead
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
coothead
post Aug 6 2024, 09:26 AM
Post #3


Advanced Member
****

Group: Members
Posts: 234
Joined: 12-January 23
From: chertsey, a small town 25 miles south west of london, england
Member No.: 28,743



Hi there bibliovore,

to avoid page repeating on button click use
this revised attachment instead of the first...

Attached File  bibliovore_revised.zip ( 2.45k ) Number of downloads: 132



coothead
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
bibliovore
post Aug 6 2024, 07:33 PM
Post #4





Group: Members
Posts: 2
Joined: 5-August 24
Member No.: 29,217



Thank you so much, that's amazing!

I can't figure out how to get it to work in my Weebly website (I'm not particularly knowledgeable in web design), but I think I found a Youtube vid that will help.

Thanks again!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
7 User(s) are reading this topic (7 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 13th December 2024 - 08:03 AM