JQuery.load () responds to an error in Firefox, works fine in Chrome

I have a function to open a page in a dialog box instead of the main window. The slightly cleared code is as follows:

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 (status == "error") runs under Firefox. It looks like there is a 404 error in Firefox, it could be an image of a loaded page or something like that.

Any ideas how to get Chrome behavior under Firefox too? (You can find a working example here )

+3
source share
4 answers
  • In FireFox, window.location.origin - undefined. Firefox therefore tires to get the page:

    http://openerp.co.hu/hu/funkcionalis-bemutato/undefined/static/docs/sales.html

    and does not work

  • chrome, window.location.origin http://openerp.co.hu. :

    http://openerp.co.hu/static/docs/sales.html

, window.location.origin, :

window.location.protocol + "//" + window.location.host
+10

firefox window.location.origin ( )

TL;DR

:

var $window_location_origin = window.location.protocol+'//'+window.location.host;

window.location.origin aka window.location.protocol+'//'+window.location.host. , .

window.location.protocol+'//'+window.location.host.length - http://25, window.location.host, .

, , :

var $window_location_origin = window.location.protocol+'//'+window.location.host;

$window_location_origin, 25 (window.location.host.length) 7 window.location.protocol+'//', 32.

+1

Any error message in particular? Also, update the code as follows:

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]
    });
}
0
source

404 means "page not found".

Set a breakpoint and check the URL that is causing the problem. Is it really?

Chrome may be more lenient when it comes to illegal characters in a URL other than Firefox, or something like that. Try pasting the URL into the location bar in both browsers to see what you get.

0
source

All Articles