0

After a lot of research, i finally found how to set the initial scale of a webpage using javascript for example in Chrome DevTools. The code should be like this:

document.getElementById("viewport").setAttribute("content", "initial-scale=0.5, user-scalable=yes");

But, I´m always getting error: Uncaught TypeError: Cannot read property 'setAttribute' of null(…) I tried this on different pages. Is there someting that has changed? I found this method in different posts.

Josch Hazard
  • 323
  • 3
  • 20

2 Answers2

1

The error message indicates that there is no element with an id of "viewport" on the page. You may be looking to modify the attributes on the viewport meta tag instead, in which case this answer describes the right approach using JavaScript.

buttonupbub
  • 195
  • 1
  • 7
  • Thank you (both) very much. It working. Calling `document.getElementById("viewport");`, the viewport-content has changed. However, the scale doesn´t change. What I was trying to achieve, is something like `document.body.style.zoom = '0.8';` on [this](https://coinmarketcap.com/currencies/bitcoin/) website. (changing the scale instantly. But if I call zoom, the sliders of the timeintervall selector aren´t working anymore. But I guess, this question here is answered. – Josch Hazard Jul 12 '17 at 22:45
1

I tried first using document.getElementById("viewport"); and later, using your full code and it worked.

Here is the screenshot result:

sample screenshot results

So, maybe try this. Call document.getElementById("viewport"); and if if is not null or undefined, execute:

document.getElementById("viewport").setAttribute("content", "initial-scale=0.5, user-scalable=yes");
Mauricio Arias Olave
  • 2,259
  • 4
  • 25
  • 70
  • Thank you (both) very much. It working. Calling `document.getElementById("viewport");`, the viewport-content has changed. However, the scale doesn´t change. What I was trying to achieve, is something like `document.body.style.zoom = '0.8';` on [this](https://coinmarketcap.com/currencies/bitcoin/) website. (changing the scale instantly. But if I call zoom, the sliders of the timeintervall selector aren´t working anymore. But I guess, this question here is answered. – – Josch Hazard Jul 12 '17 at 22:47