Hello, allow me to explain my situation in more detail.
I have three versions of my site’s homepage. One for higher resolution screens (1600 width or higher) one for lower res screens (below 1600 width) and one for tablets. Now you’re probably wondering, why the hell is that necessary? Well, it’s complicated and rather than get lost in the backstory, I want to stay focused on the main issue:
I want create a page before the homepage (let’s call it a pre-homepage) that says: “for optimal viewing please choose your device” and then have two linked buttons one that says “Desktops and Laptops” and the other “Tablets”. I want the “Desktops and laptops” button to automatically detect the user’s screen resolution and redirect them to the appropriate page.
I’ve tried searching for example code on google and all the examples haven’t worked.
I would very much appreciate any help I can get on this. Thanks!
I have not tried it as I don’t know how to implement it. I see it has some code for getting the user’s screen size, but I don’t know what the correct code is to tell wix what to do with that screen size once it has been obtained. I should also have pointed out in my original post that I have very little experience in code…
The problem with this is that not all desktop users have the same screen resolution. I need some sort of code that says… if desktop user’s screen resolution is less than x, redirect to y if resolution is more than x redirect to z.
Based on Josh’s reply below I wonder if I could do some sort of combination of yours and his reply. something like if windowWidth > 1600 then wixLocation.to “homepage a” else wixLocation.to “homepage b” Same problem though, I don’t know how to write the code to bring that idea together and make it work.
@tomheuser83 my advice would be to do what 90% of websites do and just have two versions: large screen and small screen. That’s what users expect. Wix is very very good at resizing content between tablet/desktop size devices (though I agree it’s often handy to be able to redirect users to a seperate mobile site). However, the above code is super simple and it would only take you an hour or so at the very most (even if you are a complete beginner) to adapt it.
[@tom.heuser83] The only way to do this at the detail you need is to use an htmlComponent that determines the resolution and then sends a message to the page telling it what resolution it has detected.
$w.onReady(function () {
// when a message is received from the HTML Component
$w(“#myHtmlComponent”).onMessage( (event) => {
console.log(Screen resolution: height:${event.data.height}, width:${event.data.width} );
} );
} );
The first section of code is html and you put this in your html component. Make sure your component is called “#myHtmlComponent”.
First of all, thanks for your replies! And secondly, apologies for my naivete, but where is the HTML component? I see two options in the wix code editor: “Site Code” and “Page Code”.
Thanks! Now that we have code that gets and tells the page what the user’s screen resolution is, how do I instruct the page to redirect the user according to their resolution?
@Steve Cropper Actually, I take back what I said. What I provided above will not do. Window width and height only give the size of the browser window, not screen resolution. So I’m back to square one.
I tried to make use of your code with the HTML component and it doesn’t work so far. Here’s what I have:
(Note well, I named my HtmlComponent “#html1”
import wixLocation from ‘wix-location’;
$w.onReady( function () { // when a message is received from the HTML Component
$w(“#html1”).onMessage( (event) => { console.log(Screen resolution: height:${event.data.height}, width:${event.data.width} ); } ); } );
wix sites automatically resize themselves. depending on window size. So even if you managed to fix your code, you’re going to have to write much much more code to overwrite the Wix default coding for resizing elements (MUCH more). And in any case, why does screen resolution matter when users could be browsing in any window size?
@88josh I was trying to work around the issue of writing code to resize elements by going the screen resolution route. Let me try to explain with a bit more back ground. Basically i want to use an old photograph of a street corner and have the signs be clickable buttons (elements) that take the user to other pages on the site. If I set the photograph as the page background the buttons will change size and position based on the the user’s screen resolution and/or window size, which I don’t want. Here’s a live example of that: https://www.capitolhillpast.org/photo-as-background Even though I put the buttons right over the signs in the editor, they are no longer where I put them when I view the live version and they move even more when I change the window size.
The alternative was to insert the photograph as an image floating over a blank background. This way you can create a different version of the homepage for every type of screen resolution with the appropriately sized image and buttons. Here’s one example using my resolution 1920x1080 https://www.capitolhillpast.org/photo-as-image The buttons stay put over the signs as I want them to as long as you are operating within 1920x1080–at least so it appears for me anyway.
The problem with this is that I haven’t figured out the code and it’s a pain to create a page for EVERY possible screen resolution and to code for it accordingly . Conclusion: the fun idea of having clickable signs probably just isn’t an option on Wix.
@tomheuser83 that’s a cute photo and a great idea for a site. Have you tried affixing all the elements to the top left hand corner of your screen? You’d still need to create individual version for mobile and big screen.
@88josh I have not. Do you mean just the buttons while the photo is set as background? Or the buttons AND the photo as a floating image? Either way. Having trouble imagining what that accomplishes.
@tomheuser83 Okay, I’ve investigated a little and I understand the problem you are having. I think you are right, the clickable signs idea is going to be very complicated. However, the good news is that I think the version here https://www.capitolhillpast.org/photo-as-image works fine! As the sign is in the centre of the image, even people with resolutions around half the size of yours will still see it.
If i were you, however, I would use the code I posted to make a mobile version (perhaps just using the signpost part of the image).
it’s a really great photograph and the signs idea is really good, so it would be a shame not to use it.
@88josh problem with that is for ton of people a large portion of the image and title will end up being cropped out of view which wont look good. The other workaround for this is to choose a middle ground size that will result in higher resolution users seeing blank bands on either side of the photo.
Aside from this I was not planning on using the signs as buttons in mobile version as it would be too small.