Получить класс body родительского окна из iframe
Здравствуйте.
Мне нужно скриптом, который выполняется в iframe получить класс тэга body родительского окна. Пытаюсь делать так: var cl = window.parent.document.body.getAttribute("class"); но получаю ошибку Blocked a frame with origin ... from accessing a cross-origin frame. Добавил в iframe sandbox="allow-scripts allow-same-origin" Вроде бы это должно разрешить выполнение скриптов, находящихся внутри iframe в родительском окне. Но ничего не меняется, та же ошибка. Добавил для контроля console.log("window = ", window); console.log("window.parent = ", window.parent); console.log("window.parent.document = ", window.parent.document); console.log("window.parent.document.body. = ", цindow.parent.document.body); Получил window = Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: global, …} window.parent = global {window: global, self: global, location: Location, closed: false, frames: global, …} В firefox ошибка Permission denied to access property "document" on cross-origin object В Chrome ошибка Blocked a frame with origin ... from accessing a cross-origin frame. Как мне получить нужную мне информацию? |
Цитата:
Видать страница-то не твоя. Или с содержимым ифрейма в разных доменах... Цитата:
|
Да, родительское окно не мое.
Но сейчас для эксперимента сделал html файл с таким содержимым: <!DOCTYPE_html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> </head> <body class="www theme-dark ccc"> <iframe src="путь к java web приложению, запущенному под Tomcat" sandbox="allow-same-origin allow-scripts allow-top-navigation" style="width:600px; height:600px"></iframe> </body> </html> В приложении под томкатом запускается следующий скрипт: <script type="text/javascript"> var theme = ""; console.log({window: window}); console.log("window = ", window); console.log("window.parent = ", window.parent); console.log("window.top = ", window.top); console.log("window.top.body = ", window.top.body); var cl = window.parent.document.body.getAttribute("class"); console.log("cl = " + cl); if(cl == null) cl = "theme-dark"; var arr = cl.split(" "); for(i = 0; i < arr.length; i++){ console.log(arr[i].substring(0,6)); console.log("Проверка = " + (arr[i].substring(0,6) == "theme-")); if(arr[i].substring(0,6) == "theme-"){ theme = arr[i].substring(6); break; } } location.href = "/theme/" + theme; </script> Ошибка та же самая |
Цитата:
|
Часовой пояс GMT +3, время: 04:41. |