jQuery.load() responds with error under Firefox, works fine under Chrome
I have a function to open a page in a dialog box instead of the main window. A bit cleaned up code is the following:
var baseurl = window.location.origin + '/static/docs/'
function onClickLink(event) {
event.preventDefault();
if ($("#dialog").length == 0) {
setUpDialog()
}
var href = event.target.href;
href = baseurl + href.substring(1 + href.lastIndexOf('/'));
$("#dialog").load(href + ' .body', function(response, status, xhr) {
if (status == "error") {
window.location = event.target.href;
} else {
changeImageSrc();
reStructure();
}
});
$("#dialog").dialog({
modal: true,
title: event.target.text,
width: 960,
position: ['center', 100]
});
}
This code works fine in Chrome, but th开发者_Go百科e (status == "error") is executed under Firefox. Seemingly there is a 404 error for Firefox, might be an image of the loaded page, or something similar.
Any ideas how to get the Chrome behavior under Firefox too? (you can find a working example here)
In FireFox, window.location.origin is
undefined
. FireFox therefore tires to get the page:http://openerp.co.hu/hu/funkcionalis-bemutato/undefined/static/docs/sales.html
and fails
In chrome, window.location.origin
http://openerp.co.hu
. Chrome ties to get the page:http://openerp.co.hu/static/docs/sales.html
and succeeds
Instead of relying on window.location.origin
, try using:
window.location.protocol + "//" + window.location.host
why firefox doesn't support window.location.origin (it's not standard)
tl;dr
sometimes you need this instead of the previously selected answer:
var $window_location_origin = window.location.protocol+'//'+window.location.host;
explanation
I need to get the length of window.location.origin
aka window.location.protocol+'//'+window.location.host
. Simply replacing the former with the latter, doesn't work.
window.location.protocol+'//'+window.location.host.length
will return something like http://25
, which is the protocol and the length of window.location.host
concatenated on the end.
I got around this by making a variable, like so:
var $window_location_origin = window.location.protocol+'//'+window.location.host;
After that, I could get the length of $window_location_origin
which would be that original 25 (window.location.host.length
) plus the 7 from window.location.protocol+'//'
, giving me the desired 32.
Any error message in particular? Also, update your code with the ones below:
var baseurl = window.location.origin + '/static/docs/';
function onClickLink(event) {
event.preventDefault();
if($("#dialog").length==0) {
setUpDialog();
}
var href = event.target.href;
href = baseurl + href.substring(1+href.lastIndexOf('/'));
$("#dialog").load(href + ' .body', function(response, status, xhr) {
if (status == "error") {
window.location = event.target.href;
} else {
changeImageSrc();
reStructure();
}
});
$("#dialog").dialog({
modal:true,
title:event.target.text,
width: 960,
position: ['center', 100]
});
}
404 means "page not found".
Set a breakpoint and check the URL which causes the problem. Is it really valid?
Maybe Chrome is more lenient when it comes to illegal characters in the URL than Firefox or something like that. Try to paste the URL into a location bar in both browsers to see what you get.
精彩评论