I have a javascript file in whcih I am trying to add "meta" and "css" tag. I am getting the error as "Syntax error". Below is the code tried:
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = 'myCss';
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'site url/css/CustomStyle.css';
link.media = 'all';
var meta = document.createElement('meta');
meta.name = 'viewport';
meta.initial-scale = '1';
meta.content = 'width=device-width';
var link1 = document.createElement('link');
link1.id = 'BootstrapmyCss';
link1.rel = 'stylesheet';
link1.type = 'text/css';
link1.href = 'site url/css/bootstrap-responsive.min.css';
link1.media = 'all';
head.appendChild(link);
head.appendChild(link1);
head.appendChild(meta);
I am getting an error in the line as "Syntax error"
meta.initial-scale = '1';
How to fix this? Thanks
I have a javascript file in whcih I am trying to add "meta" and "css" tag. I am getting the error as "Syntax error". Below is the code tried:
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = 'myCss';
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'site url/css/CustomStyle.css';
link.media = 'all';
var meta = document.createElement('meta');
meta.name = 'viewport';
meta.initial-scale = '1';
meta.content = 'width=device-width';
var link1 = document.createElement('link');
link1.id = 'BootstrapmyCss';
link1.rel = 'stylesheet';
link1.type = 'text/css';
link1.href = 'site url/css/bootstrap-responsive.min.css';
link1.media = 'all';
head.appendChild(link);
head.appendChild(link1);
head.appendChild(meta);
I am getting an error in the line as "Syntax error"
meta.initial-scale = '1';
How to fix this? Thanks
Share Improve this question edited Sep 18, 2018 at 9:16 KARTHIKEYAN.A 20.2k10 gold badges137 silver badges150 bronze badges asked Apr 20, 2017 at 8:12 venkat14venkat14 6233 gold badges14 silver badges35 bronze badges 3-
2
Try:
meta["initial-scale"] = '1'
– Titus Commented Apr 20, 2017 at 8:14 -
2
You can't have dashes in a variable names. Try to use
meta['initial-scale']
. – Ivar Commented Apr 20, 2017 at 8:14 - 1 Possible duplicate of How do I reference a javascript object property with a hyphen in it? – JJJ Commented Apr 20, 2017 at 8:16
4 Answers
Reset to default 4you can replace meta.initial-sacle = '1';
with meta['initial-scale'] = '1';
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = 'myCss';
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'site url/css/CustomStyle.css';
link.media = 'all';
var meta = document.createElement('meta');
meta.name = 'viewport';
meta['initial-scale'] = '1';
meta.content = 'width=device-width';
var link1 = document.createElement('link');
link1.id = 'BootstrapmyCss';
link1.rel = 'stylesheet';
link1.type = 'text/css';
link1.href = 'site url/css/bootstrap-responsive.min.css';
link1.media = 'all';
head.appendChild(link);
head.appendChild(link1);
head.appendChild(meta);
You can not use -
as a part of variable name, because it is interpreted as minus (Subtraction) operator.
To fix this, use can use setAttribute
method, like this:
meta.setAttribute('initial-scale', '1');
or just replace that line with meta['initial-scale'] = '1';
meta['initial-scale'] = '1';
not valid on typescript
way.
Try this.
const meta = document.createElement('meta');
meta.name = 'viewport';
meta.content = 'width=device-width, initial-scale=1';
const head = document.getElementsByTagName('head');
if (head) {
head[0].appendChild(meta);
}