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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    ajax/jquery development pattern

    I am a Java developer who is working more and more with javascript. I love Jquery, but am unsure how to architect a largish ajax project.

    Each widget I make will need to communicate with the server, so I have put each widget in its own directory and have it pull dynamic content from files in the same directory.

    When I want a page (in another directory) to include the widget, I have taken the approach of including the appropriate .js file, creating the objects defined within it, then having the widget write its content to a div on the current page. The problem I am running into is that the objects created from the imported .js files don't seem to know which directory they came from anymore, so they cannot find the server side files they need to communicate with.

    What is the best way to deal with this? Is there a better way to architect this? Is there a way that objects loaded from a .js file can know what their original source directory is?

    - Steve

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    JavaScript files do not know where they come from. You will have to code the file structure some how into your JavaScript code.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by A1ien51 View Post
    JavaScript files do not know where they come from. You will have to code the file structure some how into your JavaScript code.
    Eric
    hard-coding paths makes the scripts very brittle.

    based on you description of the task, you can easily detect the current script's file as it loads by using the function below.

    Code:
    function currentScript(){
        var t = document.getElementsByTagName("script");
     return t[t.length-1];
    }
    
    //example:
    alert(currentScript().src);

    note that once your page loads, aliens51's advice is correct; code itself doesn't know where it's from...
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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