JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 16th, 2013, 10:14 PM
Avtar9 Avtar9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 46 Avtar9 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 7 h 57 m 7 sec
Reputation Power: 0
Other - Can't Get This Random Video Generator To Work

Alright, so I just want an array with embed codes of YouTube videos and each pageload loads a different one from the video. Shouldn't be too complex yeah?

Here's the code I'm using, but it won't work. Can someone help me out please?

Code:
<script type="text/javascript"> 

var video = newArray(); 
var select=0; 

video[0] = "<iframe width="560" height="200" src="http://www.youtube.com/embed/7Am7i7uM9r0" frameborder="0" allowfullscreen></iframe>"; 

video[1] = "<iframe width="560" height="200" src="http://www.youtube.com/embed/ceBXUyuv4Q0" frameborder="0" allowfullscreen></iframe>"; 

video[2] = "HTML EMBED CODE HERE"; video[3] = "HTML EMBED CODE HERE"; 

select = Math.floor(Math.random() * video.length); document.write(video[select]); 

</script>

Reply With Quote
  #2  
Old January 16th, 2013, 10:27 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 599 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
I changed a few things; but it's less recursive this way.

Code:
<iframe id="my_video_player" width="560" height="200" frameborder="0" allowfullscreen></iframe>

<script type="text/javascript"> 

var video = new Array(); 
video[0] = "http://www.youtube.com/embed/7Am7i7uM9r0";
video[1] = "http://www.youtube.com/embed/ceBXUyuv4Q0";

var maxVideo = video.length - 1;

(function() {
var selection = Math.round(Math.random() * maxVideo);
document.getElementById("my_video_player").src = video[selection];
})();

</script>

Reply With Quote
  #3  
Old January 16th, 2013, 10:39 PM
Avtar9 Avtar9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 46 Avtar9 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 7 h 57 m 7 sec
Reputation Power: 0
Not working man. Nothing shows up on the page.

Reply With Quote
  #4  
Old January 16th, 2013, 10:41 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 599 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
Tested it, before I posted it... it works fine; copy and paste the code... just like I have it.

Reply With Quote
  #5  
Old January 16th, 2013, 10:45 PM
Avtar9 Avtar9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 46 Avtar9 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 7 h 57 m 7 sec
Reputation Power: 0
Done.

Works just fine, apologies.

Thanks mate.

Reply With Quote
  #6  
Old January 16th, 2013, 10:51 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 599 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
No problem, glad you got it working.
Comments on this post
Avtar9 agrees: Thanks for the help!

Reply With Quote
  #7  
Old January 17th, 2013, 01:43 AM
Avtar9 Avtar9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 46 Avtar9 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 7 h 57 m 7 sec
Reputation Power: 0
Quick question though, how come the initial code wasn't working?

Reply With Quote
  #8  
Old January 17th, 2013, 12:51 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 599 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
Quote:
Originally Posted by Avtar9
Quick question though, how come the initial code wasn't working?


You had a few errors, that needed to be corrected; for your code to work. Here is the corrected version of your code:

Code:
<script type="text/javascript"> 

var video = new Array(); // you had a syntax error... you had it like this: "newArray()"

var select = 0;

video[0] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/7Am7i7uM9r0" frameborder="0" allowfullscreen></iframe>';

video[1] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/ceBXUyuv4Q0" frameborder="0" allowfullscreen></iframe>'; 

video[2] = 'HTML EMBED CODE HERE 1';

video[3] = 'HTML EMBED CODE HERE 2';

video.length = video.length - 1; // video.length would be 4; so you have to subtract it by one... so you do not get an "undefined" error

select = Math.floor(Math.random() * video.length);

document.write(video[select]); 

</script>


Security Note: If I were you; I would not use document.write() no more then I had to though (I actually do not use it at all); as it can be a browser securtiy risk for XSS injections.

Reply With Quote
  #9  
Old January 17th, 2013, 08:30 PM
Avtar9 Avtar9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 46 Avtar9 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 7 h 57 m 7 sec
Reputation Power: 0
Quote:
Originally Posted by web_loone08
You had a few errors, that needed to be corrected; for your code to work. Here is the corrected version of your code:

Code:
<script type="text/javascript"> 

var video = new Array(); // you had a syntax error... you had it like this: "newArray()"

var select = 0;

video[0] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/7Am7i7uM9r0" frameborder="0" allowfullscreen></iframe>';

video[1] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/ceBXUyuv4Q0" frameborder="0" allowfullscreen></iframe>'; 

video[2] = 'HTML EMBED CODE HERE 1';

video[3] = 'HTML EMBED CODE HERE 2';

video.length = video.length - 1; // video.length would be 4; so you have to subtract it by one... so you do not get an "undefined" error

select = Math.floor(Math.random() * video.length);

document.write(video[select]); 

</script>


Security Note: If I were you; I would not use document.write() no more then I had to though (I actually do not use it at all); as it can be a browser securtiy risk for XSS injections.


Thanks man, losing the hang a little of JavaScript and because of C++ and what not I tend to always put the semicolon after statements, haha.

Appreciate the help, that explains a lot. Was getting a little worried about what was wrong.

A simple document.write like that could be an XSS loophole? How so?

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Other - Can't Get This Random Video Generator To Work

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap