Skip to content

JSON Stringify changes time of date because of UTC

My date objects in JavaScript are always represented by UTC +2 because of where I am located. Hence like this Problem is doing a JSON.stringify converts the above date to What I need is for the date and time to be honoured but it’s not, hence it should be Basically I use this: I tried passing a replacer…

Resetting a setTimeout

I have the following: How can I, via a .click function, reset the counter midway through the countdown? Answer You can store a reference to that timeout, and then call clearTimeout on that reference.

Capture javascript event in IE Mobile

I need to detect the id of the element that generated an onchange event. This code work in most modern browsers: But it does not work in IE Mobile. I have tried the following code, and at least the event is fired and the handler function is called, but window.event is not available when event handler is calle…

Javascript : Change the function of the browser’s back button

Is there a way to make the user’s back button on their browser, call a javascript function instead of going back a page? Answer You can’t override the behaviour that if a user follows a link to your page, clicking Back will take them off it again. But you can make JavaScript actions on your page a…

How do I set/unset a cookie with jQuery?

How do I set and unset a cookie using jQuery, for example create a cookie named test and set the value to 1? Answer Update April 2019 jQuery isn’t needed for cookie reading/manipulation, so don’t use the original answer below. Go to https://github.com/js-cookie/js-cookie instead, and use the libra…

Preventing auto-creation of global variables in Javascript

I just spent some time debugging a problem that boiled down to forgetting to use the var keyword in front of a new variable identifier, so Javascript was automatically creating that variable in the global scope. Is there any way to prevent this, or change the default behavior, without using a validator like J…

How to extract base URL from a string in JavaScript?

I’m trying to find a relatively easy and reliable method to extract the base URL from a string variable using JavaScript (or jQuery). For example, given something like: http://www.sitename.com/article/2009/09/14/this-is-an-article/ I’d like to get: http://www.sitename.com/ Is a regular expression …

Disable/enable an input with jQuery?

or Which is the standard way? And, conversely, how do you enable a disabled input? Answer jQuery 1.6+ To change the disabled property you should use the .prop() function. jQuery 1.5 and below The .prop() function doesn’t exist, but .attr() does similar: Set the disabled attribute. To enable again, the p…