HELP! the following code retrieves the same device “Desktop” when viewing the site on my laptop, the Editor Preview and my iPhone 6 ( all devices using Chrome ). I need my page to hide elements when viewed on mobile and if I hide the elements in the Mobile Editor all my code breaks as it tries to reference elements that are not there (this also happened with the horizontal menu… had to recreate it with buttons as the menu disappears from the mobile editor).
Is there something wrong with the function or is it my code?
import wixWindow from 'wix-window';
let formFactor = wixWindow.formFactor; // "Mobile" or "Desktop"
if (formFactor === "Mobile") {
console.log(formFactor);
$w('#logoHeader').show();
$w('#logoHeaderBW').hide();
$w('#menuHeader').hide();
$w('#menuHeaderGroup').hide();
$w('#menuHeaderCloseGroup').hide();
}
Hey
You can’t hide elements on mobile in the editor because then they are not present in the DOM and they will break the code. How do you test the console log in the mobile device? The formfactor should work as you coded as long as the objects are present on the page and in the DOM.
Hey Andreas, I included the console log to see if the mobile editor showed something different (it doesn’t). I know it’s not working on mobile because I assigned the formFactor variable to a text element an de it says “Desktop” when viewed on my mobile.
Hey,
When you hide an element, it still exists in the DOM, it’s just not visible . Therefore, I’m not sure that I fully understand what you were referring by saying that the code breaks as it tries to reference elements that are not there. Moreover, note that you can also hide element on mobile editor without using code as explained here .
If this does not resolve the issue, please provide us with the site URL so that we can have a look.