Jump to content

Cross browser getById function


bennyboywonder

Recommended Posts

I am trying to make a cross-browser getById and getStyle function to use with my other javascripts;

 

at present to use these in conjunction with each other, I would have to write it like this

 

var mystyle = getStyle(getById("addcat"))

I think

function getById(whichLayer)
{
    this.getStyle = getStyle;
    object = document.getElementById ? document.getElementById(whichLayer)
           : document.all ? document.all[whichLayer]
           : document.layers ? document.layers[whichLayer]
           : false;
    return object
}
function getStyle(obj)
{
    var object = obj.style ? obj.style : obj;
    return object;
}

is their a way of writing an object so that I could use standard dot syntax, ie

var mystyle = getById("addcat").getStyle;

 

I was thinking something along the lines of

var browsers = new browserObject();

var mystyle = browsers.getById("myid").style;
mystyle.left = "10";
function browserObject()
{
    this.getById = function(whichLayer)
    {
        this.style = getStyle;
        object = document.getElementById ? document.getElementById(whichLayer)
               : document.all ? document.all[whichLayer]
               : document.layers ? document.layers[whichLayer]
               : false;
        return object
    }
}
function getStyle(obj)
{
    var object = obj.style ? obj.style : obj;
    return object;
}

 

though this is just a guess of the sort of thing I am after, and I know this doesn't work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.