| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| New Member Join Date: Dec 2010
Posts: 11
![]() | Javascript Assistance Hey everyone, I'm fairly new (and I mean, completely new) to Javascript programming. I have a tiny little bit of experience in other languages (most predominantly html/css) and have come to a point where I can't seem to find an answer anywhere for this. What I'm hoping to do is have a Javascript read from a custom array (that I would create) using the 11 character codes for youtube videos, randomly select one such code from that array, and use it to display an embedded video, in effect selecting and displaying a random video from a custom created list. I'm looking to do this without resorting to PHP. So far I've had no luck, and I will provide here the code for you to see. Here is the Javascript at its source; http://yourjavascript.com/26101752210/rndvid.js and for those of you who would prefer to read it in the forum, here it is; Code: function randomVideo(){
//format for calling pop up window
vid = new Array();
vid[0] = 'X3iFhLdWjqc';
vid[1] = 'ADC3SLx6HV8';
var ranNum = Math.floor(Math.random() * vid.length);
var movielink;
movielink = "http://www.youtube.com/v/" + vid[ranNum] + "&rel=1";
var mainlink = "";
mainlink = "<object width='425' height='355'>";
mainlink = mainlink + "<param name='wmode' value='transparent'></param>";
mainlink = mainlink + "<embed src='" + movielink + "' type='application/x-shockwave-flash' wmode='transparent' width='425' height='355'></embed>";
mainlink = mainlink + "</object>";
document.getElementById('movietext').innerHTML = mainlink;
document.write(mainlink);
return false;
} ????: NamePros.com http://www.namepros.com/programming/690509-javascript-assistance.html Here is the HTML that I place on the page to attempt to have the video displayed; Code: <script type="text/javascript" src="http://yourjavascript.com/26101752210/rndvid.js"> randomVideo(); </script> All help and advice is greatly appreciated, thank you! |
| | |
| | #2 (permalink) |
| NamePros Regular Join Date: Jan 2007 Location: Namepros
Posts: 240
![]() ![]() ![]() | I would be very interested to know where you found this source code. In this case you need to add the 'movietext' id where you want the video to display, so getElementById can find it. something like this will work: Code: <div id="movietext"></div>
__________________ "Because it really is that simple imho. If you don't want sheep for users, don't treat your users like sheep." Memory Chips | Euro ETF | Flowers | A1c Levels & Diabetes |
| | |
| | #4 (permalink) |
| NamePros Regular Join Date: Jan 2007 Location: Namepros
Posts: 240
![]() ![]() ![]() | so here I spend the time to help you and you won't answer my simple question? ?
__________________ "Because it really is that simple imho. If you don't want sheep for users, don't treat your users like sheep." Memory Chips | Euro ETF | Flowers | A1c Levels & Diabetes |
| | |
| | #5 (permalink) | ||||
| NamePros Member Join Date: Dec 2008
Posts: 59
![]() ![]() |
| ||||
| | |
| | THREAD STARTER #6 (permalink) | ||||||||
| New Member Join Date: Dec 2010
Posts: 11
![]() |
????: NamePros.com http://www.namepros.com/showthread.php?t=690509
there is html code to embed it provided in this topic but I found that the iframe method was unsuccessful. | ||||||||
| | |
| | #7 (permalink) |
| NamePros Regular Join Date: Jan 2007 Location: Namepros
Posts: 240
![]() ![]() ![]() | ok thanks for the response, appreciated ![]() ????: NamePros.com http://www.namepros.com/showthread.php?t=690509 here's what I would do: up in the <head> portion of your HTML page, put the call that loads the .js file remotely. Then, down in the body of your page, call the function with javascript. That function uses a standard technique of writing its output to a particular ID that is specified in the CSS. Wherever you choose to place the <div id="movietext"></div> on your HTML page, that is the spot where the js function is going to write the embed code that shows the video.
__________________ "Because it really is that simple imho. If you don't want sheep for users, don't treat your users like sheep." Memory Chips | Euro ETF | Flowers | A1c Levels & Diabetes |
| | |
| | THREAD STARTER #8 (permalink) | ||||
| New Member Join Date: Dec 2010
Posts: 11
![]() |
This also was not successful which leads me to believe the javascript is the problem... Anybody know what might be wrong with it? | ||||
| | |
| | #9 (permalink) |
| NamePros Member Join Date: Mar 2010
Posts: 124
![]() | Above, you have the randomVideo() call inside the <script> tag, which won't work. You need to make sure it's called after the movietext div is rendered in your browser, so try putting it either in the body after the movietext div or in an onload event.
__________________ DirectWWW - an OpenSRS reseller |
| | |
| | #10 (permalink) |
| NamePros Regular Join Date: Apr 2006
Posts: 360
![]() ![]() ![]() ![]() | I just ran the code you had and it worked almost perfect for me, you have to make sure you have the following: Code: <div id="movietext"></div> ????: NamePros.com http://www.namepros.com/showthread.php?t=690509 Code: function randomVideo(){
//format for calling pop up window
vid = new Array();
vid[0] = 'X3iFhLdWjqc';
vid[1] = 'ADC3SLx6HV8';
var ranNum = Math.floor(Math.random() * vid.length);
var movielink;
movielink = "http://www.youtube.com/v/" + vid[ranNum] + "&rel=1";
var mainlink = "";
mainlink = "<object width='425' height='355'>";
mainlink = mainlink + "<param name='wmode' value='transparent'></param>";
mainlink = mainlink + "<embed src='" + movielink + "' type='application/x-shockwave-flash' wmode='transparent' width='425' height='355'></embed>";
mainlink = mainlink + "</object>";
document.getElementById('movietext').innerHTML = mainlink;
return false;
} Cheers, Jay
__________________ Canadian Domain Registrar Ready.ca |
| | |
| | THREAD STARTER #12 (permalink) |
| New Member Join Date: Dec 2010
Posts: 11
![]() | It would actually appear I still need some help, I want to have mutliple of these items appear on a single page, but adding additional randomVideo(); commands, adding new <script> tags, and adding new <div> tags do not work. It will only let me have one (or any multiples display the same video) I want all of them to draw from the same array. What should I do? |
| | |
| | #13 (permalink) |
| NamePros Member Join Date: Mar 2010
Posts: 124
![]() | One possibility would be to change: Code: function randomVideo(){ Code: function randomVideo(id){ Code: document.getElementById('movietext').innerHTML = mainlink; Code: document.getElementById(id).innerHTML = mainlink; Code: <div id="movietext1"></div>
<div id="movietext2"></div>
<div id="movietext3"></div>
<script type="text/javascript">
randomVideo('movietext1');
randomVideo('movietext2');
randomVideo('movietext3');
</script>
__________________ DirectWWW - an OpenSRS reseller |
| | |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Small Javascript Project | Rudy | Web Development Wanted | 1 | 09-07-2008 10:07 AM |
| JSeditor.com (JavaScript editor) | edder | Domains For Sale - Fixed Price | 0 | 12-12-2007 09:42 PM |
| Wanted: Javascript expert | cvxdes | Web Development Wanted | 0 | 03-25-2006 06:37 AM |
| Adding Javascript to pre-existing Javascript Page | nigelwong | Programming | 0 | 12-16-2005 06:20 AM |