S
SM
Hello,
I've created this 'wonderful' function the embeds a youtube video in a
specified div section using the Javascript DOM. Everything works OK...
until I realize how bad the logical programming was.
See, if you look at the function below, everytime i passed the youtube
video id, i create the object over and over and over .... again and
again and again..... you get the picture.
I could erase the object using [div.innertHTML = '' ] and created
again, but that's even worst! 10 times worst!!!!!!!!!!
These is probably a better logic:
Using an if statement, I want to check if the object exists. If not
created. If exist, then just replace the attribute 'value'.
Now for my question:
How do i get a handle to the object so i can verify if the object
exists or not?
Thanks
Marco
function video(id)
{
var w = 350;
var h = 288;
div = document.getElementById('mydiv'); //insert the newly created
object here
var obj = document.createElement('object');
obj.setAttribute('width', w);
obj.setAttribute('height', h);
var param = document.createElement('param');
param.setAttribute('name', 'movie');
param.setAttribute('value', 'http://www.youtube.com/v/' +id);
obj.appendChild(param);
var param = document.createElement('param');
param.setAttribute('name', 'wmode');
param.setAttribute('value', 'transparent');
obj.appendChild(param);
var embed = document.createElement('embed');
embed.setAttribute('width', w);
embed.setAttribute('height', h);
embed.setAttribute('wmode', 'transparent');
embed.setAttribute('type', 'application/x-shockwave-flash');
embed.setAttribute('src', 'http://www.youtube.com/v/' +id);
obj.appendChild(embed);
div.appendChild(obj);
}
I've created this 'wonderful' function the embeds a youtube video in a
specified div section using the Javascript DOM. Everything works OK...
until I realize how bad the logical programming was.
See, if you look at the function below, everytime i passed the youtube
video id, i create the object over and over and over .... again and
again and again..... you get the picture.
I could erase the object using [div.innertHTML = '' ] and created
again, but that's even worst! 10 times worst!!!!!!!!!!
These is probably a better logic:
Using an if statement, I want to check if the object exists. If not
created. If exist, then just replace the attribute 'value'.
Now for my question:
How do i get a handle to the object so i can verify if the object
exists or not?
Thanks
Marco
function video(id)
{
var w = 350;
var h = 288;
div = document.getElementById('mydiv'); //insert the newly created
object here
var obj = document.createElement('object');
obj.setAttribute('width', w);
obj.setAttribute('height', h);
var param = document.createElement('param');
param.setAttribute('name', 'movie');
param.setAttribute('value', 'http://www.youtube.com/v/' +id);
obj.appendChild(param);
var param = document.createElement('param');
param.setAttribute('name', 'wmode');
param.setAttribute('value', 'transparent');
obj.appendChild(param);
var embed = document.createElement('embed');
embed.setAttribute('width', w);
embed.setAttribute('height', h);
embed.setAttribute('wmode', 'transparent');
embed.setAttribute('type', 'application/x-shockwave-flash');
embed.setAttribute('src', 'http://www.youtube.com/v/' +id);
obj.appendChild(embed);
div.appendChild(obj);
}