Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getElementById in external javascripts

    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
    Rich S. Wyatt
    D3 Web Creations

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Do you have a link so we can see the page in question for ourselves?

  • #3
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Rich S. Wyatt
    D3 Web Creations

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeċ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts

    Trying out the new "Phrase Center" WA added for staff...

    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to call the code via window.onload.

    Code:
    window.onload = function() {
      document.getElementById('someID') ...
    }
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Location
    Des Moines, Iowa
    Posts
    219
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!
    Rich S. Wyatt
    D3 Web Creations

  • #7
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    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).

  • #8
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    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.

  • #9
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Beagle is correct.

    rswyatt, you're welcome

    Quote Originally Posted by Bill Posters
    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.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #10
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by Kravvitz
    *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:
    Last edited by Bill Posters; 04-07-2006 at 08:09 AM.

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by rswyatt
    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...
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •