This error only occurs in the Opera browser. I've seen a few posts with a similar error, but i don't think the suggested solutions apply to my example. Most solutions say x is undefined. I don't think that applies, since i defer my script.
My html file:
<head>
...
<script src="media.match.min.js"></script>
<script defer src="bgimg.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<div id="container"></div>
</body>
media.match.min.js
is a polyfill for window.matchMedia()
for old browsers.
bgimg.js
is my own script which updates the background image according to media queries.
I'll just post the entire first part of bgimg.js
since it's in such an early phase (media queries omitted):
var container = document.getElementById("container");
// declare all media queries
var mqXS = window.matchMedia("...");
var mqS = window.matchMedia("...");
var mqM = window.matchMedia("...");
var mqL = window.matchMedia("...");
var mqXL = window.matchMedia("...");
var mqXXL = window.matchMedia("...");
var mqXXXL = window.matchMedia("...");
// create matching arrays to iterate
var queryLists = [mqXS, mqS, mqM, mqL, mqXL, mqXXL, mqXXXL];
var fileNames = ["_xs", "_s", "_m", "_l", "_xl", "_xxl", "_xxxl"];
// function to check media queries and load matching image
var checkQueries = function() {
for (var i = 0; i < queryLists.length; i++) {
if (queryLists[i].matches) {
var fileName = "url(images/ls/01" + fileNames[i] + ".jpg)";
container.style.backgroundImage = fileName;
break;
} else {
console.log("no match");
}
}
};
// run once
checkQueries();
So this works in all modern browsers, but throws this error in Opera:
Unhandled Error: Cannot convert 'container' to object
on line 27:
container.style.backgroundImage = fileName;
So maybe Opera has a bug up its butt about how i created fileName
? Or is something else wrong with my script? Again, no errors are thrown in any other browser.