1

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

KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
venkat14
  • 583
  • 3
  • 12
  • 34
  • 2
    Try: `meta["initial-scale"] = '1'` – Titus Apr 20 '17 at 08:14
  • 2
    You can't have dashes in a variable names. Try to use `meta['initial-scale']`. – Ivar Apr 20 '17 at 08:14
  • 1
    Possible duplicate of [How do I reference a javascript object property with a hyphen in it?](http://stackoverflow.com/questions/7122609/how-do-i-reference-a-javascript-object-property-with-a-hyphen-in-it) – JJJ Apr 20 '17 at 08:16

4 Answers4

4

you can replace meta.initial-sacle = '1'; with meta['initial-scale'] = '1';

Kermit
  • 1,062
  • 6
  • 10
3
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); 
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
1

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';

Dan Cantir
  • 2,915
  • 14
  • 24
0

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);
}
Binh Ho
  • 3,690
  • 1
  • 31
  • 31