Skip to content
Advertisement

Modifying location.hash without page scrolling

We’ve got a few pages using ajax to load in content and there’s a few occasions where we need to deep link into a page. Instead of having a link to “Users” and telling people to click “settings” it’s helpful to be able to link people to user.aspx#settings To allow people to provide us with correct links to sections (for

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 parameter (second

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 called: Is there

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 add entries into the

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 library there that doesn’t depend on jQuery. Basic examples: See the docs on

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 JSLint? Running a validator in between writing

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 the best bet? If so, what statement could I use to assign the base URL extracted from a given string to a

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 proper method is to use .removeAttr() In any version of

How to remove the hash from window.location (URL) with JavaScript without page refresh?

I have URL like: http://example.com#something, how do I remove #something, without causing the page to refresh? I attempted the following solution: However, this doesn’t remove the hash symbol # from the URL. Answer Initial question: or both will return the URL without the hash or anything after it. With regards to your edit: Any change to window.location will trigger a

Advertisement