Saturday, September 21, 2013

Getting Page URL using Javascript



Getting Page URLs using Javascript is very useful in real life projects, it's been used by Facebook, Twitter for their share/tweet buttons, because when we click on the share button then we get the URL of the page we are browsing, and that's done using Javascript.


var url= "";
if (typeof this.href === "undefined") {
    url = document.location.toString().toLowerCase();
}
else {
    url= this.href.toString().toLowerCase();
}
Liked the post ? subscribe us with your email to get upcoming tutorials directly in your inbox:

2 comments:

  1. You shouldn't use .toLowerCase() for URLs. Linux and other Unix based web servers are case sensitive after the domain name, so http://example.com/ThisPage is not the same as http://example.com/thispage. Real world current example: http://wiki.centos.org/HowTos/Network/SecuringSSH works, but http://wiki.centos.org/howtos/network/securingssh gives their "not found" page.

    ReplyDelete
    Replies
    1. That's a point, case sensitiveness can effect a lot.

      Delete

Your Comment Will Be Visible After Approval, Thanks !