0

I want to zoom out to 85 % before the page is fully loaded. Now the code has a problem that the zoom only works if the page is fully loaded. I try to put it to the head of html, but it doenst works. Can you help me?

Here is my code:

 window.onload = function zoom(){ document.body.style.zoom = "85%" }
TEXHIK
1,3981 gold badge11 silver badges35 bronze badges
asked Jul 31, 2019 at 14:26
2
  • 2
    Check this Call js function before page loads Commented Jul 31, 2019 at 14:29
  • 1
    @dota2pro $(document).ready( is triggered when the page is fully loaded, but the OP's requirement is a bit different. So, I don't think it is dup of the linked post you mentioned. Does it? Commented Jul 31, 2019 at 14:29

3 Answers 3

2

Place this just after the opening body tag.

<script>
 document.body.style.zoom = "85%";
</script>

Without the code being wrapped in a function that is a callback to the window.onload event, it will execute as soon as it is encountered.

answered Jul 31, 2019 at 14:27
Sign up to request clarification or add additional context in comments.

2 Comments

@AnatoliiParubets I'm sorry, I don't know what you mean by "it will doing your html dirty". There is nothing wrong with this HTML. This is how to make code execute as soon as it is encountered, when no event exists.
This will not work in head. The body element hasn't been parsed at that point, so it's not part of the DOM.
0

You can use DOMContentLoaded event. It is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

For example

document.addEventListener('DOMContentLoaded', () => {});
answered Jul 31, 2019 at 14:32

1 Comment

It's unclear that DOMContentLoaded is what the OP wants. Depending on what/how many/how big the external resources are, there could be an insignificant time difference between this and load.
0

Thanks very much. It works but somehow I have to wait until the page is fully load, then I can see that it zoom out to 85 %. So the picture looks like it jumping.

document.addEventListener('DOMContentLoaded', () => {});

answered Jul 31, 2019 at 18:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.