...

View Full Version : getElementById in external javascripts



rswyatt
04-06-2006, 03:19 PM
Hello all - I have a perplexing problem here and it may very well be that I don't have any idea what I'm doing - but I'm trying. :-)

I have an external .js that has a simple function in where I'm trying to gather the offsetHeight of my container div.

If I run the function within <head> in the html - works fine. I get "Object Required" when I try to run it in the external js... I've tried placing the <script></script> tag that calls the external js in various places on the page to no avail.

What am I missing here?

Thanks,
Rich

Bill Posters
04-06-2006, 03:22 PM
Do you have a link so we can see the page in question for ourselves?

rswyatt
04-06-2006, 03:37 PM
It is for an intranet site that has no public IP. :(

The funny thing is that I have a cookie writer that is external that can succesfully gather the screen.width and screen.height values. I don't get why I'm having problems gathering values for page elements.

Here is what I'm trying to accomplish:

I'm trying to write a cookie (so that I can gather the value via PHP later) because I have a site that requires a 3-4 column layout... I have to take the content and dynamically figure out how many characters can fit in a column. I'm hoping to get some values (because we all do not have standard resolutions) so that I can correctly lay the content out on the page for the different resolutions.

liorean
04-06-2006, 03:46 PM
It's hard to help when you give us almost nothing to work with. I posted a thread How to make it easy for us to help a while ago, I suggest you go read it and try to follow it.

Kravvitz
04-06-2006, 03:47 PM
You need to call the code via window.onload.


window.onload = function() {
document.getElementById('someID') ...
}

rswyatt
04-06-2006, 03:51 PM
Kravvitz... That worked! Thank you.

I actually tried it a little different and it failed - worked your way.

I tried window.onload = myFunction();

function myFunction(){

}

This produced an error but again - your way worked. Thank you much!

Bill Posters
04-06-2006, 04:34 PM
I find it odd that simply deferring the execution by using window.onload does the trick, as the same code reportedly did the job ok when used in the head (before any elements had loaded).

Beagle
04-06-2006, 04:45 PM
I tried window.onload = myFunction();

function myFunction(){

}

The reason this didn't work is because you assigned the return value of myFunction to window.onload, not the function itself.

When the engine encounters:

myFunction();

It executes the function. What you were looking for is:

window.onload = myFunction;

That would copy the value of the object "myFunction" to the onload property of the window object.

Kravvitz
04-07-2006, 06:50 AM
Beagle is correct. :)

rswyatt, you're welcome :)


I find it odd that simply deferring the execution by using window.onload does the trick, as the same code reportedly did the job ok when used in the head (before any elements had loaded).
*shrugs* Accessing any elements before window.onload is called is risky.

Bill Posters
04-07-2006, 09:06 AM
*shrugs* Accessing any elements before window.onload is called is risky.
I would have thought it would have been equally impossible given that both code versions (embedded and external) presumably referenced an object which hadn't yet been created, so therefore didn't exist in the DOM.
The fact that it reportedly worked when embedded suggested to me that the timing of the reference wasn't the issue.

Makes me wonder if some browsers might actually load documents in 'streams' rather than a single pass reading top-to-bottom in strict order.

Still, as you say… :shrug: :)

Kor
04-07-2006, 09:22 AM
If I run the function within <head> in the html - works fine. I get "Object Required" when I try to run it in the external

That is weird. Technically, there is no difference if the code is embeded or external, if they come in the same place in page. If the missing of an onload was the cause of the problem, the code shouldn't have worked neither embeded, nor external...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum