Call JavaScript function defined in iframe in Chrome using file protocol

This question is very similar to the completely updated version of the question asked here: How to call the JavaScript function from one frame to another in Chrome / Webkit with the file protocol - unfortunately, this question has never been answered.

I have an HTML page containing an SVG image in an iframe. SVG exports a JavaScript API that allows it to do useful things (reset to enlarge and center, display at "actual size"). Below the iframe, I added buttons that the user can click on this call using the functions defined in SVG.

My code is as follows:

function reset() {
  document.getElementByID('iframe').contentWindow.reset();
}

It works great in Safari, Firefox, and even in IE 9 (which supports SVG - hooray!). But in Chrome this fails: the debugger tells me that:

Property 'reset' of object [object DOMWindow] is not a function.

And indeed, it seems to be true: although contentWindow is of type DOMWindow, it has no methods or fields (at least not what the debugger will show me). Even querying the "document" field does not work (gives a null value).

It seems like using the file: // protocol to port both containing HTML and containing SVG. As noted in the question I referenced above, Chrome produces the following error when trying to access contentWindow content:

Attempt to access frame with URL file://[...]/contained.svg from frame with URL file://[...]/container.html. Domains, protocols and ports must match.

, , ; , . , , : , , .

- . " Chrome - ".

?

+3
2

, :) . - , .

- " " , , , :

// By default, file:// URIs cannot read other file:// URIs. This is an
// override for developers who need the old behavior for testing.
--allow-file-access-from-files

, Chrome : chrome.exe --allow-file-access-from-files, .

+5

, @Mohamed Mansour, .

Chrome JavaScript , [Chromium bug 4197, Chromium bug 47416].

, , Chromium , . Gecko , : [ Mozilla 230606, . , , Chrome — Chromium bug 47416, , , .

- "" — ; , . . Chrome, , , "-".

, - , Chrome, , , , , Chromium bug 47416. , Chrome, , . " Chrome", .

+2

Source: https://habr.com/ru/post/1794573/


All Articles