Other - Can't Get This Random Video Generator To Work
Discuss Can't Get This Random Video Generator To Work in the JavaScript Development forum on Dev Shed. Can't Get This Random Video Generator To Work JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
Posts: 46
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?
Posts: 599
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.
Posts: 46
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?