...

View Full Version : ajax/jquery development pattern



pilot7
12-01-2008, 04:21 PM
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

A1ien51
12-01-2008, 10:55 PM
JavaScript files do not know where they come from. You will have to code the file structure some how into your JavaScript code.

Eric

rnd me
12-02-2008, 01:54 AM
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.


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...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum