How to get a handle to an object and verify if it exists?

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);
}
 
S

SM

SM said the following on 5/2/2007 2:46 AM:
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!!!!!!!!!!

Who told you that?


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?

function video(id)
{
var w = 350;
var h = 288;
div = document.getElementById('mydiv'); //insert the newly created
object here

if (document.getElementById('someID'){
var obj = document.createElement('object');

obj.id = 'someID'

div.appendChild(obj); }
}

Give it an ID attribute and check for it when you enter the function.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

Hey Randy,
It works fine, except i can't get a handle on the 2 other child object
(param and embed). I need to access their respective values.
How do i get handle to those child objects
I tried using: objElem.param.getAttribute('value'); with no success!

Thanks
Marco



---------------------------
var w = 350;
var h = 288;

divContent = document.getElementById("content");

if(document.getElementById('objectID'))
{
alert('object FOUND!');
var objElem = document.getElementById('objectID')

***** DOESNT WORK*****
var value = obj.param.getAttribute('value');
}

else
{
alert('object NOT FOUND... create please');

var obj = document.createElement('object');
obj.id = 'objectID';

obj.setAttribute('width', w);
obj.setAttribute('height', h);

var param = newElement('param', 'movie');
param.setAttribute('value', 'http://www.youtube.com/v/' +id);
obj.appendChild(param);

var param = newElement('param', '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);

divContent.appendChild(obj);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top