Problems with Chrome path - javascript

I am using jquery and running a html file on my local machine (no server).
The following works on firefox but not on chrome:
$('#result').load('test.html');
It seem to be something wrong with the path.
Anyone know how to sort this out without having to add an absolute path please?
Thanks

Ajax requests cannot be sent cross-domain. On your local machine, every request is cross-domain to the browser, so no Ajax can be used at all. Chrome is a bit more strict than Firefox here. There is no solution, you will just have to upload it to a web server or install something like Apache on your local machine for testing purposes.

Typically, AJAX requests cannot be sent cross-domain, however, if you're just looking to access a local file, you could try enabling one of Chrome's secret flags. Specifically:
--allow-file-access-from-files
With any luck, you should be able to get things working by changing your chrome shortcut to the following:
chrome.exe --allow-file-access-from-files
I'm not sure if that flag is enabled in all builds of Chrome (I am presently running chromium 10), but it definitely works: I've been using it to enable file-save access in TiddlyWiki.

Related

Run phonegap app on chrome with Cross Domain Pages

I'm developing a phonegap app for android and I need to be able to run the app on chrome. My app doesn't have any phone functionality yet. Is all Javascript, HTML and CSS.
The problem is I'm fetching some info of my server but I'm having trouble debbuging the javascript, so I wanted to test te app on the browser so I can use the developer tools, but the ajax call wont work on Chrome (It does on the phone).
Any ideas?
You need to run Chrome with the flag --disable-web-security. You can either run chrome.exe --disable-web-security from the command line (in the appropriate directory) or edit the shortcut and add it. (Assuming you're on Windows)
If running in chrome is for testing only, jprofit's solution should be good enough. However, if users need to run it, they won't start chrome with those options :) In that case, you have two solutions
Use JSONP ans script tags (this could be a lot of work and error handling is poor)
Route your calls through a proxy on the local server http://developer.yahoo.com/javascript/howto-proxy.html, http://www.daniweb.com/web-development/php/code/216729
The simplest solution for me has been to use a proxy (as #juan-mendes suggests).
I use a tiny NodeJS server called Sleight, which runs locally ( http://phonegap.com/2010/01/20/introducing-sleight/ ).

window.open("\\\\localhost\\"); not working on win7(x64) ie8

window.open("\\\\localhost\\"); is not working on ie8 on 64bit win7. ie says "make sure the path or internet address is correct". it's working on same verson of ie8 on 32 bit. any idea? or any different way to access a path like \\localhost\ using internet explorer?
i'm trying to open \\localhost like a file path. when you write \\localhost on ie, it opens "Network > localhost"
In error details, ie says "Message: Access is denied.".
It may related to security settings of windows. Any idea which settings of windows might cause that?
If you are running httpd in your host http://localhost/... must work.
Otherwise, better use file:///... to access local file.
I don't know how come \\localhost\... did work for your 32bit machine.
Maybe, IE 8 script engine did interpret that as a simple file path, and then tried to access the path from local SMB server. Did you enabled windows file sharing as you did in 32bit machine?
Anyway I do not believe that URL scheme is compatible with other browsers than IE.
It's worked when I add the site to "Trusted Sites" on ie.
Localhost requires a port number. Open the target site in the browser and get the map path. It should follow the syntax below;
port #/View Name /.cshtml file
window.open('http://localhost:****/SomeViewName/Some.cshtmlfile', '_blank' + ***.start);

How to force Chrome/Firefox/Opera to do crossdomain requests from localhost?

Hy,
So I've got this problem: I'm trying to develop an HTML file on my localhost, and all the browsers I have installed are preventing me to send cross-domain requests.
What I'm trying to do is to convert a script I wrote in Python (with Mechanize, pyquery and lxml) to Javascript, the reasons don't really matter (I thought in Javascript it would be easier to simulate something).
Cross-domain requests from local files used to work, and are still working on the latest Safari (stable), but I can't install Safari on this machine (it's a Linux box).
EDIT
I discovered the escalated privileges of Firefox, e.g.:
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
// among others
Problem is, I'm also trying to access the content loaded in an iFrame ... as I said, I'm trying to simulate a browser session, like I would with a library like Mechanize, but from Javascript. My script still works in Safari.
So how to force Chrome / Firefox / Opera to bypass all security restrictions from locally stored files?
I'm interested in a browser command-line option, I would even consider a patch to the browser (I'm the user, not trying to distribute this to anyone else).
Thanks,
I'm unsure whether or not you have control of the content in the IFrames but since you are speaking of local files I guess you do. This article on Ajaxian should cover your use case.
Since you are targeting grade A browsers (e.i. not IE) you could use html5 window.postMessage instead of IFrames.
Cheers, Jon.
I have no working knowledge of Python Mechanize lib.
Why don't you start a simple http server instead to solve your problem?
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
server_address = ('127.0.0.1', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
This python code will start a simple http server listening on port 8000 or the first argument and handles all files in the folder from this script.
I'm not sure if this works when the page is on localhost, but it might be worth a try: http://www.asual.com/jquery/address/
That jQuery plugin lets you make cross-domain requests from any domain... Perhaps it might work with localhost? Haven't tested it though.

Very strange JavaScript error in IE7

I am developing JavaScript chat. I have done the prototype, and it seems to work pretty well. But our client says that it doesn't work. We both use IE7 on PC, and try to run the same JavaScript code. I have no idea about the reason.
On my client's machine there is "object is expected" error. I have thought about security restrictions, and tried to make security level higher in my IE, but in this case the script doesn't run and there is no error messages. I am running the script on the remote server.
Clients machine may be running within a group policy which for security reasons can restrict execution of scripts. Hence the client may need to include your prototype as part of their trusted sites and relax restrictions on trusted sites
Are you able to provide any more details about the specific error? Are you sending the client a zipped up version of the solution or is it hosted somewhere?
From the IEBlog; you could try using the Microsoft Script Debugger (see post)
One thing you may find, especially with IE is the error that is reported is actually caused by a previous error in the code.
Try running your javascript through jslint to ensure that it is well formed.
In Firefox with [firebug], enable strict warnings in the console.
Related SO posts.
debugging-javascript-in-ie7.
debugging-javascript-for-ie6.
Edit::
One thing to look for is comma's after the last element in an array, IE seems to ignore the closing brace } and keep adding following code to the array. Firefox gracefully just assumes that you forgot it.
If you're running the code from localhost and he's running it from a remote server then it could be a timing issue - some script could be running before the page has finished loading that's causing a problem on a slow connection that is hidden on a fast connection.
You can get this error if you try to use an exernal script file that the client's browser can not access.
Example :
<script src="http://localhost/scripts/freelib.js" language=Javascript> </script>
You can access freelib.js on your machine, but running it from any other machine it wouldn't work.
Are you sure all the scripts are being loaded? I've seen that error message before when some or all aren't loaded (maybe they didn't get deployed). Try manually copying the addresses of all the js files from the source and pasting them into the address bar to make sure that the browser can actually access them.
Can you run the page on your client's machine in Firefox? The firefox console should give you a more detailed error message than IE will.

Can I disable SOP (Same Origin Policy) on any browser for development?

I want to develop JavaScript on my Windows machine. Do you know a browser where I can turn off Same Origin Policy so I can develop locally? Firefox would be optimal.
Or if you know a proxy I could use for a SOAP/WSDL site it would be great too.
I am trying to work with the JavaSCript SOAP Client.
UPDATE 6/2012: This used to work at the time of the writing, but obviously no more. Sorry.
In Firefox (might apply to other Gecko-based browsers as well) you can use the following JavaScript snippet to allow cross-domain calls:
if (navigator.userAgent.indexOf("Firefox") != -1) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
catch (e) {
alert("Permission UniversalBrowserRead denied -- not running Mozilla?");
}
}
It looks like there's an issue created in the Chromium issue tracker for achieving the same functionality, so you could try starting Chrome with the argument --disable-web-security. I don't know which builds this works on exactly, but at least Nokia's WRT Tools comes with a Chrome installation that does in fact allow loading content from other sites.
Unfortunately, using the following:
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
has been disabled in Firefox 5.
https://bugzilla.mozilla.org/show_bug.cgi?id=667312
Make a page on your local server that calls the remote server and answer the same as the remote server.
Example, javascript calls local server for a JSON. The local server makes the call to the remote server for that JSON. The local server receives the JSON from the remote server and send it to the javascript.
Using the Chromium 13.07, you can start it with security disabled:
/usr/bin/chromium-browser --disable-web-security
That's on Ubuntu 11, but change the location as your system.
All of the given answers are good ones when it comes to getting around the same origin policy in production.
For development, there is no convenient way to "disable" this security check. There are workarounds (see other answers) or hacks (you could use Greasemonkey to wrap up the JavaScript and use their GM_xmlhttprequest as a temporary measure), but no way to actually "turn it off" as you describe.
i run this command on mac, it works on me when i use google chrome to run my project.
open -a Google\ Chrome --args --disable-web-security --user-data-dir
I have no real experience with this, but FireFox 3.5 allows Cross-Site JS according to the W3C Cross-Origin Resource Sharing Draft.
See: https://developer.mozilla.org/En/HTTP_access_control
Firefox would be optimal.
If you can live with Internet Explorer, you may be able to use an .hta application
http://msdn.microsoft.com/en-us/library/ms536496(VS.85).aspx
(This is one of the ways the Selenium test automation tool deals with the issue)
In Chrome (& Chromium) 48 and above you should add the flag --user-data-dir like this:
chromium-browser --disable-web-security --user-data-dir
And it works.
You can also redirect a local port to the remote server and port via ssh.

Categories