Friday, 22 April 2016

Modify the URL without reloading the page

This can now be done in Chrome, Safari, FF4+, and IE10pp4+!
 function processAjaxData(response, urlPath){
     document.getElementById("content").innerHTML = response.html;
     document.title = response.pageTitle;
     window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
You can then use window.onpopstate to detect the back/forward button navigation:
window.onpopstate = function(e){
        document.getElementById("content").innerHTML = e.state.html;
        document.title = e.state.pageTitle;

how to get current URL in jQuery/JavaScript?

To get the path, you can use:
var pathname = window.location.pathname; // Returns path only
var url      = window.location.href;     // Returns full URL

How to include a JavaScript file in another JavaScript file?

The jQuery library provides loading functionality in one line:
$.getScript("my_lovely_script.js", function(){

   alert("Script loaded but not necessarily executed.");


if element is hidden checking by jQuery

// Checks for display:[none|block], ignores visible:[true|false]

Page redirect using jQuery

jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.

It is better than using window.location.href =, because replace() does not keep the originating page in the session history, meaning the user won't get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href. If you want to simulate an HTTP redirect, use location.replace.
// similar behavior as an HTTP redirect

// similar behavior as clicking on a link
window.location.href = "";

Edit an incorrect commit message in Git

Amending the most recent commit message

git commit --amend
Will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:
git commit --amend -m "New commit message"
…however, this can make multi-line commit messages or small corrections more cumbersome to enter.
Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

Changing the message of a commit that you've already pushed to your remote branch

If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with
git push <remote> <branch> --force
# Or
git push <remote> <branch> -f
Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.
Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronise their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.

Use interactive rebase

Another option is to use interactive rebase.
This allow you to edit any message you want to update even if its not the latest message
In order to do a git squash follow those steps:
// X is the number of commits to the last commit you want to be able to edit
git rebase -i HEAD~X
Once you squash your commits - choose the e/r for editing the message


How do you undo the last commit?

down voteaccepted

Undo a commit and redo

$ git commit -m "Something misspelled"              (1)
$ git reset --soft HEAD~                                    (2)
<< edit files as necessary >>                               (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5)
  1. This is what you want to undo
  2. This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message1, or both. Leaves working tree as it was before git commit.
  3. Make corrections to working tree files.
  4. git add whatever changes you want to include in your new commit.
  5. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEADcommit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option instead.

Alternative content