# Preventing JS from loading in the Page Editor

**URL:** <https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650>\
**Category:** CMS Development\
**Tags:** javascript\
**Created:** [October 9, 2019, 10:57am UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650 "2019-10-09T10:57:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![eldin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/eldin/32/13631_2.png) [@eldin](https://community.hubspot.com/u/eldin)\
**Post date:** [October 9, 2019, 10:57am UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/1 "2019-10-09T10:57:59Z")

</div>

I have inserted a [Slick](https://kenwheeler.github.io/slick/) carousel into a page template in the “Additional markup before \</body\>”. It works great while viewing the page, but the problem I am facing is in the editor, the script also runs when rendering the page therefore preventing me from editing the original content as the slick construct modifies the content to display it as a carousel (not desired).

I have attempted to prevent the script from running by checking if the body tag contains the hubspot-disable-focus-styles class, but the condition seems to be ignored when rendering in editor.

When running the script from the console, it does correctly detect the class and returns the correct boolean from the console in both the editor and out of editor page render. The if statement appears to be ignored and the script runs disregarding if the condition was met or not and triggers the slick construct from within the page editor.

```auto
jQuery(document).ready(function(){
  if( jQuery('body.hubspot-disable-focus-styles').length ) {
     return false;
  } else {
     jQuery('.slick-group').slick();
  }
});

```

---

<div class="post-metadata">

**Author:** ![eldin](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/eldin/32/13631_2.png) [@eldin](https://community.hubspot.com/u/eldin)\
**Post date:** [October 9, 2019, 2:57pm UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/2 "2019-10-09T14:57:20Z")

</div>

The solution has been provided by the support team, they pointed me to this article.

> **[Accounts Dashboard | HubSpot](https://app.hubspot.com/myaccounts?next=developerdocs&redirect=%2Fcms%2Fguides%2Fprovide-a-good-editor-experience)**

How to use window.hsInEditor provided the answer.

In my instance it was just a matter of checking window.hsInEditor in the conditional.

```auto
jQuery(document).ready(function(){
 if( window.hsInEditor ) {
 return;
 } else {
 jQuery('.slick-group').slick({});
 }
});

```

Worked a treat.

---

<div class="post-metadata">

**Author:** ![sharonlicari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sharonlicari/32/101479_2.png) [@sharonlicari](https://community.hubspot.com/u/sharonlicari)\
**Post date:** [October 10, 2019, 3:43pm UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/3 "2019-10-10T15:43:34Z")

</div>

Thank you for sharing this @eldin 🙂

---

<div class="post-metadata">

**Author:** ![ojobson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ojobson/32/84083_2.png) [@ojobson](https://community.hubspot.com/u/ojobson)\
**Post date:** [January 18, 2024, 11:07am UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/4 "2024-01-18T11:07:36Z")

</div>

This prevents the code running in the editor, but if you do not have jQuery on your website, then the code will also not run on the web page. An error is thrown because jQuery is unknown.

I tried wrapping it in a try / catch statement, but that still ran in the editor (although the code does now work on the web page).

---

<div class="post-metadata">

**Author:** ![ojobson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ojobson/32/84083_2.png) [@ojobson](https://community.hubspot.com/u/ojobson)\
**Post date:** [January 18, 2024, 11:12am UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/5 "2024-01-18T11:12:23Z")

</div>

Solution found!! ✅

I have amended the recomended code on the support article to start the window onload check with native javascript (instead of jquery) and have tested it and it works well.

```lia-code-sample
<script>
window.onload = function() {
 if (window.hsInEditor) { 
 return; //stops running here when in the editor
 } else {
 //this code runs on your live website, but not in the editor
 }
}
</script>

```

---

<div class="post-metadata">

**Author:** ![ACBlack](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/acblack/32/30191_2.png) [@ACBlack](https://community.hubspot.com/u/ACBlack)\
**Post date:** [October 3, 2024, 2:51pm UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/6 "2024-10-03T14:51:31Z")

</div>

This works, but there’s a Hubl solution:

```lia-code-sample
{% if !is_in_editor %}
// code snippets or files that need NOT loading when in page editor
{% endif %}

```

---

<div class="post-metadata">

**Author:** ![LJanssens](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@LJanssens](https://community.hubspot.com/u/LJanssens)\
**Post date:** [May 16, 2025, 9:51am UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/7 "2025-05-16T09:51:47Z")

</div>

@ACBlack, where do you need to add this to work?

---

<div class="post-metadata">

**Author:** ![Albusis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/albusis/32/44597_2.png) [@Albusis](https://community.hubspot.com/u/Albusis)\
**Post date:** [May 16, 2025, 10:11am UTC](https://community.hubspot.com/t/preventing-js-from-loading-in-the-page-editor/16650/8 "2025-05-16T10:11:37Z")

</div>

This comes to the module into the HTML/HuBL part since in the JS part you cannot use HubL.
