M Schenkel
M Schenkel

Reputation: 6364

js "scripts" folder name prepended to XMLHttpRequest calls

I have a javascript file named myscripts.js in the "scripts" folder of my webserver. It could be accessed with this:

http://www.example.com/scripts/myscripts.js

Within myscripts.js is a javascript function which makes a XMLHttpRequest call to somemethod.html of my website. Here is the calling code:

xmlhttp.open("GET","somemethod.html",false);

99% of the time everything works fine. But I am finding some browsers are prepending "scripts/" to the call. So the result is a call like this:

http://www.example.com/scripts/somemethod.html

when it should be this:

http://www.example.com/somemethod.html

This is a custom built webserver (i.e. I basically handle ALL requests).

  1. Should my webserver be able to handle this? Or is this just some fluky browser that I should not worry about?
  2. Should I not be using "relative" paths in the javascript? And instead use absolute calls in the java script? e.g.: instead of "somemethod.html" it should be coded like this:

    xmlhttp.open("GET","http://www.example.com/somemethod.html",false);

Upvotes: 1

Views: 104

Answers (2)

NMunro
NMunro

Reputation: 910

I never use relative requests, I built my own url handing js code to build up and pass urls around with a 'toString' method to give me exactly the url I need.

Also as an aside, try not to use synchronous XHR calls anymore, ideally you should use async and call backs, it's a pain, but it's for the best.

client . open(method, url [, async = true [, username = null [, password = null]]])

    Sets the request method, request URL, and synchronous flag.

    Throws a "SyntaxError" exception if either method is not a valid HTTP method or url cannot be parsed.

    Throws a "SecurityError" exception if method is a case-insensitive match for `CONNECT`, `TRACE` or `TRACK`.

    Throws an "InvalidAccessError" exception if async is false, the JavaScript global environment is a document environment, and either the timeout attribute is not zero, the withCredentials attribute is true, or the responseType attribute is not the empty string. 

source: http://xhr.spec.whatwg.org/#the-open%28%29-method

Upvotes: -1

T.J. Crowder
T.J. Crowder

Reputation: 1074495

It's absolutely fine (and overwhelmingly the standard of practice) to use relative paths in the JavaScript, just be aware of what they're relative to: The document in which you've included the JavaScript (not the JavaScript file.) You seem clear on this, but just emphasizing.

I've never seen a browser get this wrong. It's possible the requests you're seeing are from a poorly-written web crawler looking at the source of the JavaScript rather than doing something intelligent like figuring out where/how it's run.

Just for clarity, though, about the relative thing (more for lurkers than for you):

Given this structure:

foo.html
index.html
js/
    script.js

In that structure, if you include script.js in index.html:

<script src="js/script.js"></script>

...then use code in that script file to do an XHR call, the call will be relative to index.html, not script.js, on a correctly-functioning browser.

Upvotes: 2

Related Questions