Edit
I originally phrased this question as if browser detection was causing my problem, but it was simply caused by incorrect syntax.
- Make sure to close all brackets (they should always come in pairs!)
- Give your code some space (it will be easier to spot mistakes!)
- Clean up your
if-statements by moving messy conditional expressions into variables - Use semantic (ideally unambiguous) variable names — this makes it easier to understand what your code is trying to do
For example, the following:
const layoutStyles = document.querySelector('#layoutStyles');
const testElement = document.createElement('div');
let supportsFlex = testElement.style.flex !== undefined;
let supportsFlexFlow = testElement.style.flexFlow !== undefined;
if (supportsFlex && supportsFlexFlow) {
layoutStyles.setAttribute('href', 'flex-layout.css');
} else {
layoutStyles.setAttribute('href', 'float-layout.css');
}
is technically equivalent to this:
const cond = document.querySelector('#cond');
const tstEl = document.createElement('div');
if (tstEl.style.flex!==undefined&&tstEl.style.flexFlow!==undefined)cond.setAttribute('href','flex-layout.css');
else cond.setAttribute('href','float-layout.css');
but the latter is much harder to read and understand. Thus it is also harder to debug and refactor, and easier to accidentally introduce errors.
As the contributors wrote in their answers long ago, it’s important to learn language basics; crawl before you walk, walk before you run!
For some reason, this portion of javascript is blocking out all the other functions I have written previously. They work if I take it out, and stop working when I put it back in.
If someone can spot what’s wrong, please let me know! I’m not very good with JS really, so I’m not quite 100% sure what I’m even doing here.
Well, I used this as my reference; http://www.javascriptkit.com/javatutors/navigator.shtml
Here’s my code:
//4th batch shows a warning box upon finding a visitor with an outdated browser
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Opera/x.x or Opera x.x (ignoring remaining decimal places);
var oprversion=new Number(RegExp.$1) // capture x.x portion and store as a number
if (ffversion<11 || ieversion<9 || oprversion<11.6)
function browserwarning()
{
document.getElementById('oldbrowser').style.display="inherit";
}
//END OF 4TH BATCH
You forgot some closing brackets and function inside if-statesment are invalid. This code will work without syntax errors:
But I highly recommend you to to some efforts in learning how to use and write javascript code. It looks like you are trying to write a letter in chinese although you don’t even know their symbols…