September 11, 201115 yr This may be a really stupid way of going about this, but unfortunately it's the only thing I could come up with. So for some odd reason the horizontal scrollbar is coming up on a site I'm making and I don't want it to be there, but I still wanted it there when people had smaller browser windows,so I thought I'd use some JavaScript! I've currently got this code: var winW = 630, winH = 460; if (document.body && document.body.offsetWidth) { winW = document.body.offsetWidth; winH = document.body.offsetHeight; } if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) { winW = document.documentElement.offsetWidth; winH = document.documentElement.offsetHeight; } if (window.innerWidth && window.innerHeight) { winW = window.innerWidth; winH = window.innerHeight; } if (winW > 640){ document.getElementById("html").innerHTML( style='overflow-x:hidden'); $(":first").setAttribute(" style='overflow-x:hidden'"); } Which is supposed to add style='overflow-x:hidden' to my HTML tag if the window width is greater than 640px... It doesn't. Anyone got a fix (Or another way to go about fixing this problem)? Thanks in advance
September 12, 201115 yr Hi there, I think the problem is to do with the use of innerHTML which will expect a string to be passed. innerHTML will essentially inject an html string into the selected element: // Example document.getElementById("html").innerHTML = '<div id="test"></div>'; I haven't tested this but I'd say that the innerHTML in your script would cause an error which could result in nothing happening in your page. You may also need to change the setAttribute piece too: //from: $(":first").setAttribute(" style='overflow-x:hidden'"); //to $(":first").style="overflow-x:hidden"; I'm not 100% on this one though, my javascript is a little rusty. I hope this helps.
September 12, 201115 yr Author I actually fixed it If anyone's interested about how to get the horizontal scroll bar on a site to only show up when the screen size is smaller than x use THIS: var winW = 630, winH = 460; if (document.body && document.body.offsetWidth) { winW = document.body.offsetWidth; winH = document.body.offsetHeight; } if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) { winW = document.documentElement.offsetWidth; winH = document.documentElement.offsetHeight; } if (window.innerWidth && window.innerHeight) { winW = window.innerWidth; winH = window.innerHeight; } if (winW<900) document.getElementById("html").removeAttribute("style") Where the 900 on the last line represents the amount of pixels in width you want. Hope that helps everyone that didn't know how (like myself )
Create an account or sign in to comment