简体   繁体   中英

Javascript best practice: Calling a file from different locations in a function

I have a problem with a javascript file that includes a function. I have 2 files, that include the javascript file and that included function. The structure could look this way for example:

/var/www/file1.jsp
/var/www/content/file2.jsp

both include the file include.js with the function doSomething() The function is calling another file that is located on the server via GET. For example the file is located in /var/www/index.jsp

How would be the best way to call the index.jsp out of the function with a relative path (absolute pathes are not possible in this context). The problem is that file1.jsp and file2.jsp have a different context from which they include the .js file and so the current path for the function is different in both cases.

Assuming index.jsp is always in the same place, I would parse the absolute url from window.location.href and construct my path the index.jsp right there in doSomething()

EDIT:

A better way would be to let the browser do the url parsing for you . Use window.location.hostname and then concatenate with the hard-coded path to index.jsp relative to the root directory of your domain. Feed that concatenated string to your GEt request and all should be well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM