Please bare in mind that I am still learning JavaScript so be kind please.
I have the following code which searches a webpage for any CSS containing HTTP urls. However, there is one variable "v" which can sometimes be undefined. Error "rule.style is undefined"
How can I resolve this undefined response? I have tried to use conditions but with no luck.
var seachHttp = function () {
var cssSheets = document.styleSheets, // Loaded CSS Sheets
i =0, il = cssSheets.length, // Counter and limit for sheets
j, jl, rules, rule, // Counter and vars for Rules inside a Sheet
stylesToSearch = [ // Properties to Seach HTTP ON
'background',
'background-image',
],
k, kl=stylesToSearch.length, // Counter for properties
s, // Current Property
v; // Current Value
for(;i<il;i++) { // Loop Sheets
rules = cssSheets[i].rules || cssSheets[i].cssRules;
for(j=0,jl=rules.length;j<jl;j++) { // Loop Rules
rule = rules[j];
for(k=0;k<kl;k++){ // Loop Styles
s = stylesToSearch[k];
v = rule.style[s]; // Get Value from Current Style
if ( typeof v !== undefined && v.toString().toLowerCase().indexOf("http") > -1 ) { // Seach for HTTP Content
alert("Found HTTP at " + rule.selectorText + " " + s + " = " + rule.style[s]);
return true;
}
}
}
}
return false;
}
I call this function using:
var cssresult = seachHttp();
if (cssresult == true && cssresult !== undefined) {
//code here
}