JavaScript - Detecting User Screen Size
I am new to Javascripting but am teaching myself and have written this script from my portfolio site in an effort to detect the users screen resolution so that my art is viewed properly. I have ran the code through a debugger and it has comback as "free of errors" but when I run the webpage on my local machine it does nothing, the page displays as if I had written nothing and due to my limited knowledge of Javascript I am at a loss and hoping for someone to say, "look you screwed this part up fix this and it's good to go"... I'm hoping. Thank You all in advance..
Here is the code Code: <script language="Javascript"> function detect(){ if(screen.width<1280||screen.height<720){ alert("This web page is best viewed with a screen resolution of 1280 by 720 or higher. Your current resolution is "+screen.width+" by "+screen.height+". If possible please change your resolution.") } else{ alert("Whoa, you have a high resolution. Nice Job!") } } </script> Similar TutorialsHi there. I'm considering using the script below to detect the screen resolution of the user and then call a specific CSS for that user. The main aim is so that I can design the website at a fixed width for a larger resolution (used by most people), but if the user has a 980px resoltion then they can be catered for as well. Code: <SCRIPT language="JavaScript"> <!-- if ((screen.width>=1024) && (screen.height>=768)) { window.location="highres.html"; } else { window.location="lowres.html"; } //--> </SCRIPT> I don't want to use liquid layouts as I think it looks messy, and I don't want to design to a width of 960px as I don't like the blank space at the sides on larger resolutions. Is this an acceptable method, or do you think i'll run into problems? Any advice would be great. Cheers. Pat. Hi there I know this picture_viewr js is a fullscreen slide Would it be possible to set a screen size (700x500 e.g.) and then set a background color or the part that would be outside this window??? Thx a lot allberto http://www.alfaaudiovisual.com/pictu...er/ByB/ByB.htm I am using this javascript code to detect screen size and display an alternate css. Code: if (screen.width<1600) { document.write('<link rel="stylesheet" href="<?php echo $path ?>/css/templateadjust.css" type="text/css" />'); } This works fine in FF and Chrome but doesn't seem to work in IE. Any idea why? Im coding an internet browser and when I put it in full screen the web browser part is staying the same size as it was on small screen. How do I make it so when its on small screen it is the size of the window but when its on big screen its the size of the whole screen? Hi all, I'm trying to detect the screen resolution of the client's computer. I've been using the following example: PHP Code: <HTML> <TITLE>screen resolution</TITLE> <HEAD> </HEAD> <BODY> <?php if(isset($_COOKIE["users_resolution"])) $screen_res = $_COOKIE["users_resolution"]; else //means cookie is not found set it using Javascript { ?> <script language="javascript"> <!-- writeCookie(); function writeCookie() { var today = new Date(); var the_date = new Date("December 31, 2023"); var the_cookie_date = the_date.toGMTString(); var the_cookie = "users_resolution="+ screen.width +"x"+ screen.height; var the_cookie = the_cookie + ";expires=" + the_cookie_date; document.cookie=the_cookie location = 'get_resolution.php'; } //--> </script> <?php } ?> <?php echo "Your screen resolution is set at ". $screen_res; ?> </BODY> </HTML> This all works fine except it doesn't factor in if the user changes their screen resolution for any reason. In my own experience I often change resolutions depending on what I'm doing. I posted the question in there PHP forum http://www.codingforums.com/showthread.php?t=192440 but it looks like I'm needing a pure JS solution. Fou-Lu kindly offered their approach: Quote: Originally Posted by Fou-Lu As for detecting changes, only javascript can do that. Instead of using PHP to check that, use pure javascript and alter you're function to check if its cookies are not existant OR the sizes have changed compared to the cookie, update them. And as mentioned, they will still only take effect after the next page load. Could someone help me with this? I want the end result to be something like http://www.screenresolution.org/ in that if you change your resolution then refresh the page, the screen resolution is updated. I hope I've explained this clearly? Let me know if you need any more information. The next challenge is then getting my PHP functions to work in the following manner: PHP Code: if ($screen_width > 1024){ function('1280'); else { function('1024'); } Hi All, I am using jFlow slider for my website and in the head there is that function <script type="text/javascript"> $(document).ready(function(){ $("#myController").jFlow({ slides: "#slides", controller: ".jFlowControl", // must be class, use . sign slideWrapper : "#jFlowSlide", // must be id, use # sign selectedWrapper: "jFlowSelected", // just pure text, no sign auto: true, //auto change slide, default true width: "706px", height: "420px", duration: 500, prev: ".jFlowPrev", // must be class, use . sign next: ".jFlowNext" // must be class, use . sign }); }); </script> What i want to do is detect the browser window (max width 1024 ) and change the width and height attribute of this function...something like this : <SCRIPT language="JavaScript"> <!-- if ((screen.width<=1024) && (screen.height<=768)) { $(document).ready(function(){ $("#myController").jFlow({ slides: "#slides", controller: ".jFlowControl", // must be class, use . sign slideWrapper : "#jFlowSlide", // must be id, use # sign selectedWrapper: "jFlowSelected", // just pure text, no sign auto: true, //auto change slide, default true width: "706px", height: "420px", duration: 500, prev: ".jFlowPrev", // must be class, use . sign next: ".jFlowNext" // must be class, use . sign }); }); } else { $(document).ready(function(){ $("#myController").jFlow({ slides: "#slides", controller: ".jFlowControl", // must be class, use . sign slideWrapper : "#jFlowSlide", // must be id, use # sign selectedWrapper: "jFlowSelected", // just pure text, no sign auto: true, //auto change slide, default true width: "706px", height: "420px", duration: 500, prev: ".jFlowPrev", // must be class, use . sign next: ".jFlowNext" // must be class, use . sign }); }); } //--> </SCRIPT> But can't get this to work...any help please?? //Sam There is a stand-alone, full-screen slide show code published at http://www.javascriptkit.com/script/...ow/index.shtml I would like to know how to set the size of the show to less than full-screen, and how to integrate the show with other web content. Hello I want to add something to my web site. when the user clicks to download. i want to see a pop up screen which says your download has started. <script type="text/javascript"> function show_alert() { alert("Your Download Started!"); } </script> <input type="button" onclick="show_alert()" onclick="get value="Download" /> I dont get where should i put the download link Is it possible to detect keystrokes on the keyboard? I want to detect the arrow keys in particular. I'm going through the online reference links in the sticky now, great resource! Hi guys, i would like a script that detects if quicktime is installed like this one here http://developer.apple.com/internet/...lplugins.shtml but i want it to offer the link to quicktime if not installed, and continue to load the player if quicktime is installed. Im thinking something like this.. Code: <script type="text/javascript" src="http://developer.apple.com/internet/webcontent/examples/detectallplugins.shtml "></script> <script> if (detectQuickTime == true) { setTimeout('Redirect()',0); } else { setTimeout('Redirect2()',0); } function Redirect() { location.href = 'http://www.mysite.com/player.htm'; } function Redirect2() { location.href = 'http://www.mysite.com/error-install-quicktime.htm'; } </script> But i cant make it work. Can anyone help? I have an html page that contains a div that has overflow:auto. Is there a way to detect if the scrollbar is displayed on the div? If the scrollbar is present then I want a message to be displayed inside another div. If there is no scrollbar then I don't want any message to appear. Thanks My credit card processing service will only accept English characters. I need to detect entries in the form that are using non English characters. Is there a way in Javascript to create a function to force a string to UTF-8? My idea is to do something like function forceUTF8(X){ Y = ???? X; return Y; } if ( stringA != forceUTF8(stringA) alert ("You must use English Characters"); Hi, I have do a calculation when an user enters the value in the textbox during onblur event. But during the onblur event I need to capture the tabkey (keyCode) and if it is 9 do the calculate else not do that. But the problem is we cannot detect a keyCode for tab in onblur. But for sure I need to use onblur in this case and cannot use onkeydown as this will have impact on the UI screen for this particular scenario. How to go about this? in the textbox I am calling onblur = "test()" Please provide a helping hand. Thanks. Hi, Wondering if anyone can help me out with some javascript. How do I check upon clicking a random object on the page that the object onmousedown is an image or not? Any help is much appreciated. -cantonboi I am trying to detect the parent URL on a page for a Facebook application so that if the user is on my site rather than accessing the application via Facebook they will be redirected to the FB App page. In PHP I can use: if(strstr($parenturl,"mysite.com")){header("location:fbapp.com");exit;} Is it possible to do this in JS also? Because with FB Apps the URL is not always the same so instead of detecting a full URL I just want to search for my domain name within document.location.href to see if it exists and IF yes > Redirect back to FB. I want to detect the double-press of the space key. However, I want a single press of a space key to have a different function. The double-tap of space should only count if the taps of space are no more than, say, a half second apart. For example: The user hits space once, and javascript begins performing one action. 1/3rd of a second later, they hit the space bar again, and since that is within our threshold of 1/2 of a second, the action that began because the user hit the spacebar once should be cancelled, or undone (which I can write later) and the action when the user hits the spacebar twice should begin. Anyone have an idea how to do this? Thanks! function doUnload() { if (window.event.clientX < 0 && window.event.clientY < 0) { alert("Window is closing..."); } } i get window.event undefined by using var evt =window.event? event : e i get e undefuned var evt =window.event? event : e if (evt.clientX < 0 && evt.clientY < 0) { alert(evt.clientX +"Window is closing..."); } Hello there, This seems to be a bit of a sore topic on the forums but at least I think I have good reason for wanting to do it - I've written a script that displays tooltips when your mouse hovers over a link - all this is fine, but when I click on the link and move to another page, then click the back button on the browser to go back, the tooltip for the link I clicked is still visible! It'd be great if there was a way of detecting the button had been pressed so that I can hide it... p.s. I tried onclick = "tooltips.hideToolTip(4); return true;" for the links but no joy... Many thanks Edd I have an input box and I want it to function so that when the user presses the enter/return key, it runs a js function. I have the following code: Code: input.onkeydown = function () { if (event.keyCode === 13) { // do something } } This works in Safari, Opera, Chrome, and even IE, but not in firefox. Any idea how I could fix this? Thanks, Julian |