tags:

views:

65

answers:

3

In the first two paragraphs a click produces an alert box with the content of the specified file. In the second two there is no alert box.

<html><head>
<script type="text/javascript" src="../../../../resources/jquery_1-4-2.js"></script>
</head><body>
<p onclick="$.get('/home/user/0/1/2/3/4/4a.html','',function(a){alert(a);});">
Get /home/user/0/1/2/3/4/4a.html</p>
<p onclick="$.get('/home/user/0/1/2/3/4/resources/4ra.html','',function(a){alert(a);});">
Get /home/user/0/1/2/3/4/resources/4ra.html</p>
<p onclick="$.get('/home/user/0/0a.html','',function(a){alert(a);});">
Get /home/user/0/0a.html</p>
<p onclick="$.get('/home/user/0/1/2/3/resources/3ra.html','',function(a){alert(a);});">
Get /home/user/0/1/2/3/resources/3ra.html</p>
</body></html>

System and browser:

Linux Road 2.6.32-23-generic-pae #37-Ubuntu SMP Fri Jun 11 09:26:55 UTC 2010 i686 GNU/Linux
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6

The files are on the local file system. The url used is:

file:///home/user/0/1/2/3/4/test.html

The contents of each of the four files display as expected when the urls are entered into the web browser address bar by copying the urls directly from the displayed page (/home/user/0/1/2/3/4/4a.html, /home/user/0/1/2/3/4/resources/4ra.html, /home/user/0/0a.html, /home/user/0/1/2/3/resources/3ra.html).

But, accessing the file with http://localhost:8000/home/user/0/1/2/3/4/test.html, using:

python -m SimpleHTTPServer

then all four work as expected.

A: 

I'd start by closing your <p> tags, and seeing how it works then.

Also, you really should be binding events with $().bind('click', function(){}) or $().click(function(){}) rather than onclick.

Are you using Firebug, or looking at the error console?

I get a mismatched tag. Expected: </p>. error after clicking the first one. However, they all work for me.

Make sure that the files exist and the path is correct.

Here is a version with more jQuery like binding:

<html>
  <head>
    <script type="text/javascript" src="file:////var/duck/js/jquery-1.4.2.js"></script>

    <script>
      $(document).ready(function(){
        $('#p_1').click(function(){
          $.get('/home/user/whatever.html','',function(a{alert(a);});
        });
      });
    </script>
  </head>
    <body>
      <p id='p_1'>
        Get /home/user/0/1/2/3/4/4a.html
      </p>
      <p id='p_2'>
        Get /home/user/0/1/2/3/4/resources/4ra.html
      </p>
      <p id='p_3'>
        Get /home/user/0/0a.html
      </p>
      <p id='p_4'>
        Get /home/user/0/1/2/3/resources/3ra.html
      </p>
    </body>
  </html>

Fill in the binds for each <p>. There are tons of ways you could link the elements to the preferred files, actually - delegation, arrays, indexes, etc.

Alex JL
Closing <p> tags produced no change.
C.W.Holeman II
You know that not closing them is making each section part of the previous <p> elements, right? Or it would, but Firefox is fixing your HTML for you. You must close your <p> tags. This HTML is invalid. It would probably blow up worse in IE.
Alex JL
Weird, I closed them and now get `mismatched tag. Expected: </BODY>.`. Oh.. apparently FF is being case sensitive on the tags.
Alex JL
Produces the same results as the code in the question.
C.W.Holeman II
Regardless, I'd suggest working with binding events in jQuery in this style rather than onclick, which is kind of archaic. Be sure to include the `$(document).ready()`. It worked for me, so I'm thinking it just can't locate the file you're requesting.
Alex JL
+2  A: 

I may be wrong, but I have a feeling it's failing because $.get() follows the Same origin policy when processing a request and since /home/user/whatever is not a valid link and instead a filepath, it fails there.

I recommend taking the 2 minutes it takes to run a quick test server and try it with real URL's within your local domain.

If you have Python installed it's as simple as typing this in a terminal:

python -m SimpleHTTPServer
Bartek
I thought of same-domain restrictions too, and I've heard of people having problems with Ajax and file:/// vs. using an actual webserver. He does say that the first two work, though.
Alex JL
file:///home/user/0/0a.html produces the same result.
C.W.Holeman II
That's not what I told you to do?
Bartek
The two that work are at the same level or lower in the file tree while the failures require access higher than the starting directory.
C.W.Holeman II
I too strongly suggest installing (or using, one might already be installed on Ubuntu) a webserver, perhaps Apache. Developing on a webserver is a much better plan than files.
Alex JL
A: 

security.fileuri.strict_origin_policy is preventing access. To correct enter about:config in the address bar, then change the value from true to false for security.fileuri.strict_origin_policy which will grant access to all of the files using scheme file: in addition to http:.

In long, starting point for this info came from Bartek's use of the the term Same origin policy and the Wikipedia link in his answer.

From Browser Security Handbook, part 2 - project hosting on Google Code:

Firefox 3 is currently the only browser that uses a directory-based scoping scheme for same-origin access within file://. This bears some risk of breaking quirky local applications, and may not offer protection for shared download directories, but is a sensible approach otherwise.

From Mozilla Developer Center Same-origin policy for file: URIs:

Starting in Gecko 1.9, files are allowed to read only certain other files. Specifically, a file can read another file only if the parent directory of the originating file is an ancestor directory of the target file.

but,

The new security.fileuri.strict_origin_policy preference, which defaults to true, can be set to false if the user doesn't want to strictly enforce the same origin policy on file: URIs.

So, the second two files bumped into this restriction which can be addressed by changing the security.fileuri.strict_origin_policy.

Note that enabling access in other parts of the hierarchy does not grant any more access than is allowed by the file system directly.

C.W.Holeman II