0

How can i define following object properties of html and define as a function in javascript that would return some object.

HTML:

<object id="desktop" type="application/x-desktop" width="500" height="200">
<param name="client_type" value="VISTA" /> </object>

I want to acheive something like below in javascript.So that I can call GetDesktop() function from outside of javascript.

JavaScript:

 function GetDesktop()
  {
    object.id = "desktop;
    object.type = "application/x-desktop"
     ....
     ...
    ...
   }
1
  • Are you trying to create a new HTML element, or return information about one that is already on the page? Commented Apr 19, 2012 at 15:18

2 Answers 2

2

You want to return the HTML above from a JS function?

var GetDesktop = function(){
    var obj = document.createElement('object');
    obj.setAttribute('id', 'desktop');
    obj.setAttribute('type', 'application/x-desktop');
    obj.setAttribute('width', '500');
    obj.setAttribute('height', '200');
    var param = document.createElement('param');
    param.setAttribute('name', 'client_type');
    param.setAttribute('value', 'VISTA');
    obj.appendChild(param);

    return obj;
}

Obviously if any of those properties need to change, you can pass them in as parameters to the function...

Sign up to request clarification or add additional context in comments.

4 Comments

Strictly speaking, those setAttribute() calls are unnecessary. obj.id = 'desktop'; will do exactly the same thing.
Thanks tkone. That's exactly what I was looking for.Parameters will be always fixed.
@aroth They are but this way the op could easily loop through a hash of key/val pairs to set them.
@user1029083 if this solution worked for you you should click the accept button showing that it satisfied your question.
1

My interpretation of the question is the opposite of @tkone's. Hopefully one of us is right. :)

Is this browser Javascript? If so, then why not use the HTMLElement object directly?

var obj = document.getElementById('desktop');

That gets you an object for which this is true:

obj.getAttribute('id') // -> 'desktop'
obj.getAttribute('type') //-> 'application/x-desktop' 

etc.

If you want a simple JS object with attributes that are stored directly, you can create one by looping through the DOM attributes:

   var elem = document.getElementById('desktop');
   var obj = {};
   var attrCount = elem.attributes.length;
   for (var i=0; i<attrCount; ++i) {
      var attr = elem.attributes[i];
      obj[attr.name] = attr.value
   }

That gets you an object for which this is true:

obj.id // -> 'desktop'
obj.type //-> 'application/x-desktop' 

etc.

1 Comment

Thanks Mark. Yes this is browser java script.I am writing Javascript SDK API. So I want to define this in Javascript itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.