AndreaNobili
AndreaNobili

Reputation: 42957

How can I correctly discover and print the browser window width using JQuery? Why it can't work?

I am pretty new in JQuery and I have the following problem.

My page have to be differently rendered if the browser have specific width (I need it to fix an issue on mobile devices that have a smaller screen)

So as first step I am trying to discover the browser window width (so if it is less to a specific value I consider that the page is showed into a smartphone and I fix my issue).

So I am trying to do something like this:

$(document).ready(function () {
    var windowWidth = $(window).width();
    alert(windowWidth);
}

I am trying into JSFiddle link

But it seems don't work because I don't see any output. Why? What is wrong? How can I correctly discover and print the browser windows width?

Upvotes: 0

Views: 49

Answers (2)

Shrinivas Pai
Shrinivas Pai

Reputation: 7701

I think you are missing jquery also add this in your head tag.

<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>

You are missing ) add it at end of your code.

$(document).ready(function () {
    var windowWidth = $(window).width();
    alert(windowWidth);
});

Fiddle:Demo here

Upvotes: 3

Canvas
Canvas

Reputation: 5897

Hello AndreaNobili I have updated yourjsFiddle here

https://jsfiddle.net/ayjq1ke0/1/

Basically you didn't click your document ready function.

you just needed ); at the end of your code for it to work :) oohh Also in jsFiddle you need to select what plugin to load, on your jsFiddle you didn't have jQuery selected, so I have added it for ya

Upvotes: 2

Related Questions