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

3
  • 2
    Try: meta["initial-scale"] = '1' Commented Apr 20, 2017 at 8:14
  • 2
    You can't have dashes in a variable names. Try to use meta['initial-scale']. Commented Apr 20, 2017 at 8:14
  • 1
    Possible duplicate of How do I reference a javascript object property with a hyphen in it? Commented Apr 20, 2017 at 8:16

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.