Forum Moderators: open

Message Too Old, No Replies

script works only half after "async"

document.write a css does not work

         

jetteroheller

9:29 am on Apr 26, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Last statement before </BODY> is

<script scr=my.js></script>

[developers.google.com...]
told me to make this script async

No idea, why this should block rendering above the fold, but I tried it

<script scr=my.js async></script>

The script changed some <DIV> with new contence
and at last
var css =........
document.write ( css );

With async, the script still changes some <DIV>, but the document.write of the css for mobiles does not work anymore.

robzilla

9:49 am on Apr 26, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's because, when run asynchronously, the script is separated from the document. You're probably better off using something like appendChild() rather than document.write(). Even better would be to avoid JS altogether and use viewport and media queries for serving different CSS rules to mobile users.

guggi2000

10:34 am on Apr 26, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



You should not use it at the end of the body tag because the browser needs to know the basic CSS before rendering the page.
Try to break the script into 2 Javascript sections instead. Ask yourself what is necessary at the beginning and what can wait...

Whatever is displayed above the fold should be "mentioned" above the fold.