SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: javascript file

  1. #1
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javascript file

    hi everyone,

    I have a a javascript function

    <script language="javascript">
    window.onload = function()
    {
    some code
    //sets scroll postion on refresh.

    }
    </script>

    I have used this code in four web forms...it works

    Now i hve created a ScriptWindow.js were I have put the code in-cause i do not want to repeat code over again.

    Then I have called it in the head
    <script language="javascript" src="./JavaScript/ScriptWindow.js"></script>

    Ino this seems right but my function in the file dont get executed.......maybe im puttin this on in the wrong poistion......

    does any1 have an idea of y's this is happening?
    Code is Poetry

  2. #2
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the browser includes external JS source, window.onload is not executed?

    What you should do instead is set the onLoad part of the body tag to call the function as defined in the external file.

    Like so:

    <script language="javascript" src="./JavaScript/ScriptWindow.js"></script>
    <body onLoad="javascript:function();">

    and then remove "window.onload = " from the external file.

    Bold = Edit for clarity
    Last edited by darkwater23; Oct 20, 2006 at 06:16.

  3. #3
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs down

    Quote Originally Posted by darkwater23
    When the browser includes external JS source, it is not executed.

    What you should do instead is set the onLoad part of the body tag to call the function as defined in the external file.

    Like so:

    <script language="javascript" src="./JavaScript/ScriptWindow.js"></script>
    <body onLoad="javascript:function();">

    and then remove "window.onload = " from the external file.
    Say what?

    Ignore the above post, it's completely incorrect. If the code you posted is included in an external script file, it should be executed just the same as if it was in the <head> of the document. If it isn't, either you've changed the code or there must be something else wrong. Check your pathname in the <script> tag to see if you are actually loading the file.

    Can you post a link, or some more code?

  4. #4
    SitePoint Addict darkwater23's Avatar
    Join Date
    Nov 2005
    Location
    Omaha, NE
    Posts
    335
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That should have been a question mark at the end of that first sentence. He said his code didn't change, so I assumed the window.onload wasn't happening. Sorry about that. I never put onLoad event in external JS files, so it seemed logical that maybe window.onLoad wouldn't fire from there.

    Snipper, were you able to verify that the path is correct?

  5. #5
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    getting worried there, as all my window.onload's that reside in an external file work?!

    Also, have you removed the <script> tags?

    you should only have

    Code:
    window.onload = function()
    {
    // blah blah
    }


  6. #6
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nice one guys tks gRoberts as well...i had the tags in my .js file, removed them and it all worked cheers all.
    Code is Poetry


Bookmarks

Posting Permissions

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