document.stylesheet[i] returns Null information when location of stylesheet is remote

时光毁灭记忆、已成空白 提交于 2019-12-25 03:03:28

问题


What I'm trying to accomplish: A Javascript function runs on page load: It parses the first style of the site's linked stylesheet, looking for a custom property that in turn tells the javascript to run a different function (the site can pull from a large library of stylesheets and the html cannot be modified on a per stylesheet basis, and some stylesheets require the running of different javascript functions).

I thought the best method would be to use document.styleSheets[x].CSSRule[0].style in order to fetch that first style and compare it against a set of constant values to determine what javascript it should run.

This method works, however there's an issue with it. The stylesheets being pulled from the site are located on a Microsoft Azure Blob storage server, which means they aren't local.

But apparently, when fetching a remote stylesheet, the Stylesheet object that's generated from "document.styleSheets[x]" is populated with mostly null values. I was hoping to use this method as a work-around to trying to access the remote stylesheet directly since that gave me a origin-remote-access.

As you can see here, the middle two stylesheets are local. The other two (one of which contains the line of code I need) are remotely stored, and thus are populated as NULL for their properties.

Does anyone know of a way I can fetch the CSSRules for that remote stylesheet? Or at least some way I can somehow parse the first line of a remote css stylesheet and run javascript based on it's contents?

I'm pretty much limited to using javascript and jquery. Php isn't an option.

Edit: I'm guessing the issue is tied to the Same Origin Security Policy. I've noticed there may be a work around to that using Cross-Origin Resource Sharing. Does anyone know how I might implement that in order to successfully access that stylesheet's data?

来源:https://stackoverflow.com/questions/23398198/document-stylesheeti-returns-null-information-when-location-of-stylesheet-is-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!