0

I need to add my own JS to a customer's existing website. The requirement is that i can't change anything on that website.

In the website, it defines viewport inside the body tag(though it is not a good practice, but as i mentioned, we are not allowed to change anything).

<meta name="viewport" content="width=1600">

I now need to use my JS to change its viewport content to the following. So it can look better on mobile phones.

<meta name="viewport" content="width=device-width">

I tried to modify it with the following code which doesn't work.

document.addEventListener("DOMContentLoaded", function(event) {
    for(var i=0;i<document.getElementsByTagName("meta").length;i++){
        if(document.getElementsByTagName("meta")[i].name.toLowerCase()=="viewport"){
            document.getElementsByTagName("meta")[i].setAttribute("content","width=320px");
        }
    }
});
nullox
  • 295
  • 2
  • 7
  • 18
  • Possible duplicate of: http://stackoverflow.com/questions/2831529/having-trouble-using-jquery-to-set-meta-tag-values http://stackoverflow.com/questions/2568760/is-it-possible-to-use-javascript-to-change-the-meta-tags-of-the-page http://stackoverflow.com/questions/3597768/trying-to-set-metatag-dynamically http://stackoverflow.com/questions/2568760/is-it-possible-to-use-javascript-to-change-the-meta-tags-of-the-page – Jason Sep 02 '14 at 06:12
  • 1
    @moo2u2, while the threads you suggested are about how to dynamically change the meta tags, mine is how to set the viewport dynamically and for it to work(my example can change the viewport value but it doesnt take effect. – nullox Sep 02 '14 at 06:50

0 Answers0