Such a problem, how to make the script not work when a visitor comes from the phone (for example, its display width is> 767)?

This method does not help:

<script> If (document.body.clientWidth>767){ document.write('<script src="javascript.js">') } </script></script> 

    2 answers 2

     <script> if (document.body.clientWidth>800){ document.write("<script>console.log('hello!');<\/script>"); } </script> 

    This is how it works. If the screen width is small, then there is no alert. If more than 800 pixels - an alert appears. It is necessary to screen a slash <\/script> . If it is not escaped, it does not work - the browser will think that the script is a piece of code:

     <script> if (document.body.clientWidth>800){ document.write("<script>console.log('hello!');<\/script> 

    The idea that the script works with one width of the screen and does not work with the other is interesting.

    As for availWidth, this is the width of the screen in general in pixels. it never changes if the monitor is not changed. you can use innerWidth and clientWidth. here is their description. https://learn.javascript.ru/metrics-window

    • <script> if (document.body.clientWidth> 800) {document.write ("<script src =" javascript.js "> <\ / script>"); } </ script> ////////////// Doesn’t work anyway. - vladislav_zp
    • you have written "<script src =" javascript.js "> <\ / script>". the interpreter thinks that it is necessary to insert the piece "<script src =". I think that it does not work because you need to use quotation marks DIFFERENT - "<script src = 'javascript.js'> <\ / script>" - Dimon
    • I don’t know if it’s just me or it’sn’t right, but the script still doesn’t turn off .. - vladislav_zp
    • Try to include the script not from an external file, but directly into the script element. look what will happen. put a breakpoint in the script and run the code. It is necessary to debug and watch what the console writes - Dimon
    • Do you know how to use breakpoints (to find errors) browser developer tools? - Dimon

    Seems to be using

     window.screen.availWidth