www.webdeveloper.com
Results 1 to 6 of 6

Thread: Does Javascript code has to be loaded with html content?

  1. #1
    Join Date
    Feb 2011
    Posts
    67

    Does Javascript code has to be loaded with html content?

    Hi there,

    It seems Javascript code has to be loaded with the html content. If not, it will not work. Is this right? Can it be loaded in earilier load than the its content. Because of Ajax, a part of page can be loaded latter, but Javascript doessn't work for elements of the latter content. How is this handled?

    Is there an article discussing this issue?

    Thanks,

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    it's difficult to load a script without environment...
    But it's always possible to load dynamically a script like on this page...
    With Ajax, we throw generally a callback function with the return of the answer. See, for examples, this pages...

  3. #3
    Join Date
    Feb 2011
    Posts
    67
    Thanks, 007Julien. Your references are interesting. I am trying to use it to solve my repeat ajax call issue.

    I may need to give you some context to understand the issue:

    In the web page, there is navigation menu on left and a content panel on right. When you click on the left menu, the right content panel will change to an associated form. In the form, you use ajax to interact with server through javascript code. The javascript code is in an external file for handling all, including menu items. When content panel is changed by a click of the menu, the form submit will not work if the javascript code is not reloaded with the form. So the js code is included as part of the content panel. But the problem is when js code is reloaded twice, it triggers ajax call twice of the same action. So can the js code is removed before it calls ajax ro load a form?

    Thanks,

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    It's always possible to remove a script (see the link at the bottom of the preceding page). See too this page and the links at the bottom about ajax.

    Then it is only advisable to distinguish the main script (who has to contain all the features of menu and ajax call) of those specific which have to be separately (but together) loaded with the contents

  5. #5
    Join Date
    Feb 2011
    Posts
    67
    Thank you, 007Julien. These are good reading though. I may need divide the javascript code into several: one as the main is shared by all forms, others are just one per form. Hope this can solve the problem of multiple calls for one submit click.

    One thing I don't understand about javascript code is how the position of the javascipt code in the htmp page will affect its function. Some javascript code has to be put after html content. If it's put in the head section, it will not work. Why is this? Can you or some one gives me an explanation? To me, this is very bad implementation. Javascript should function the same no matter where and when it's put in the html page: head or bottom, before or latter, it should function well and same.

    Thanks,

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,252
    Placing scripts in the head makes sure that any functions are loaded before the buttons, links or other things that call them are loaded...
    On the other hand, if you put your scripts at the end of the body, the document objects exist when the script is executed and you have not to insert your opening actions in a event window.onload...
    Many other considerations can enter in account, it depend finally essentially on what makes the script !
    Last edited by 007Julien; 06-15-2012 at 08:41 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles