Ecommerce software home
Shopping Cart Software Forum for Ecommerce Templates
 
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

Find us on Facebook Follow us on Twitter View our YouTube channel
Search our site
 All Forums
 Technical
 Advanced Tips and Tricks - NO QUESTIONS PLEASE
 Change parameter based on screen width
Author « Topic »  

dbdave
ECT Moderator

USA
10276 Posts

Posted - 11/25/2017 :  18:05:50  
VERSION 3.1 (Released 11/30/2017)
Classic .asp only
Not version specific = yes
Updater proof = yes
No warranty is expressed or implied.
Not endorsed or provided by ecommerce templates.

This is somewhat based on my tip to change a parameter based on the product or category
https://www.ecommercetemplates.com/support/topic.asp?TOPIC_ID=100929
Except here we want to change a parameter based on the screen width.

For us, the reason is we want to change the order of things on the product detail page for smaller screen widths.
Primarily, there is one of the custom fields we want to show right under the product image. We are using floats to arrange things on the page and no matter what we do with the css, we cannot keep that div under the product image on both smaller screens and larger views.

There may be other parameters you want to set for smaller views as well.
For example, I have seen posts where users want to disable the softcart on mobile screens, but leave it on desktop screens. (this is what is done with the code posted)

It seems like it would be easy, but it's not. You cannot get the screen width before the .asp code runs and loads the page.
Javascript is the easiest way to check the screen size, but that will not happen until the page loads. By then, it's too late to change a parameter setting.


Version 3.1
(3.0)Allows for non javascript users (Visitors who have javascript disabled will not be able to checkout at your store, so that does not seem to be an issue. But we want to be sure search engines can index your product detail pages.)
(3.0)Allows for window resizing. Javascript will "listen" for a window resize and refresh the page after the window resize has stopped. Functionality has been added so as not to tax the server with constant resizing info as the window is being resized (debounce function).
(3.0)Redirect to another page has gone away. Now, the page is not redirected and results in almost immediate results on initial page load.
(3.0)Changed to a cookie versus session variable (nothing significant there really).
(3.1)Issue fixed on some cell phones, scrolling is treated as a "resize" event and page would potentially get caught in a loop - this has been fixed.

Thanks to a few sources
https://www.quirksmode.org/js/cookies.html (working with javascript cookies)
https://davidwalsh.name/javascript-debounce-function (debounce function reduces server load)

How does it work?
This is a combination of .asp and javascript.
The .asp runs when the page loads and checks to see if the user has a cookie set named - scrnsz
If not, it is bypassed and nothing happens there. If there is a cookie, the value (the width of your screen) will be compared to what is set and run whatever we ask it to do. In this case, set a parameter for your store.

Once the page loads, the javascript takes over.
If the cookie has not been set, then we measure the width of your screen and set a cookie with that info and refresh the page so the .asp script can get what it needs to do it's thing. On initial page load, this happens so fast, you really do not notice it.
Once this whole thing has occured, the cookie is set and as the visitor browses, this action does not run any longer.

Next we have added a feature where if the user does resize their window (unlikely, but possible that your visitor might do this) we delete the cookie and refresh the page to make sure the user is getting exactly what we want.

I have two test pages where you can see this working. (visit this link first and see if you notice that the page is being refreshed and let me know if you see any significant lag on initial load) working page showing functionality - http://www.trophykitsdev.com/dev1/proddetail-chngprmt.asp?prod=HEXNUT (soft cart is disabled on screen sizes under 500px wide)

The second page has a few alerts that show you whats happening. It actually shows you your screen width (what is set for the cookie). It has a javascript alert popup when the page refreshes and then the .asp output that shown in the upper left.
Change the screen size to see the info popup.
page with alerts showing the width of your scree and you can see whats happening - http://www.trophykitsdev.com/dev1/proddetail-alert.asp?prod=HEXNUT (soft cart is disabled on screen sizes under 500px wide)


STEP 1

Open your proddetail.asp page in your html editor

STEP 2
Insert the following in the <head> section of the page

<!-- BEGIN CHECK SCREEN SIZE AND CHANGE PARAMETER ver 3.1-->
<%
scrnresn = Request.Cookies("scrnsz")
if scrnresn >= "500" then
usehardaddtocart=FALSE
elseif scrnresn >= "200" then
usehardaddtocart=TRUE
end if
%>
<script language="javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
var scrnszval = readCookie('scrnsz');

if (scrnszval == null) {
winsize = window.innerWidth;
createCookie('scrnsz',winsize,0);
location.reload();
}
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var myEfficientFn = debounce(function() {
eraseCookie('scrnsz');
location.reload();
}, 250);

var currWidth = window.innerWidth;
window.addEventListener('resize', function(){
var nuWidth = window.innerWidth;
if(nuWidth !== currWidth){
myEfficientFn();
}
});
</script>

<!-- END CHECK SCREEN SIZE AND CHANGE PARAMETER ver 3.1 -->

STEP 3
Upload to the server.

Set the items in blue to your liking.
500 = Screen width in px
the others are parameters normally set in your includes.


The settings above will give you the softcart on screens wider than 500px and on screens smaller you get no softcart. Feel free to try it on my test page
Drag your screen wide to narrow and add to cart in both instances.
I hope some fellow store owners get some use out of it.

NOTE - The parameter you are changing does not replace the parameter set in your includes. There, you should set the parameter to what you want to happen for a "default" - what happens if javascript is not run on the clients computer.


Edited by - dbdave on 11/30/2017 22:15:08

Andy
ECT Moderator

95440 Posts

Posted - 11/26/2017 :  00:27:28  
Hi David

Thanks very much for posting - I can see how that would be handy and I'll have to try it out here.

Andy

Please feel free to review / rate our software

dbdave
ECT Moderator

USA
10276 Posts

Posted - 11/26/2017 :  17:10:48  
:::EDIT - VERSION 3.0 RELEASED - SEE ORIGINAL POST:::

search engine friendly and ready for production environment.

Edited by - dbdave on 11/29/2017 21:35:47

Andy
ECT Moderator

95440 Posts

Posted - 11/27/2017 :  01:07:20  
Thanks for all the details David. I have tested this and initially thought it wasn't working til I followed your advice

To test this properly, you would need to delete the session cookie from your browser. Then set your screen width and load a product on the product detail page.

I actually tested in a different browser with a reduced screen size to check.

Andy

Please feel free to review / rate our software

dbdave
ECT Moderator

USA
10276 Posts

Posted - 11/29/2017 :  21:37:47  
Hi Andy, I have done a total rewrite and I think I have addressed all drawbacks and I am going to move this to my live site and put it to use.
The new install is really easy and I have the two test pages where you can see it in action.

dbdave
ECT Moderator

USA
10276 Posts

Posted - 12/12/2017 :  12:25:41  
This has been verified to work just fine. We have been using it on our live store with over 1000 orders placed on just about every device you can imagine with not one single customer reported issue.

We are currently using it to change the order of items on the product details pages on smaller screens.
  « Topic »  
Jump To:
Shopping Cart Software Forum for Ecommerce Templates © 2002-2022 ecommercetemplates.com
This page was generated in 0.02 seconds. Snitz Forums 2000