Advertisements
Home > DotNet, Javascript > How to access URL or URL parts using javascript / Get the Website URL using JavaScript

How to access URL or URL parts using javascript / Get the Website URL using JavaScript


How To Get URL Parts in JavaScript :: A JavaScript Tutorial For Beginners

In some scenario we need to communicate with website url that seen in address bar of the browser using JavaScript. It’s very easy to access the url using javascript. But in order to get query string from URL, default functions or methods are not available. Using javscript we can access the URL using default method in javascript. We are not able to edit URL in javascript without postback. But we can reload the page from javascript and also we can redirect to other url from javascript. Let’s see all about jaavscript with website URL.

How to get website URL in JavaScript / how do I use javascript to get url of current page

Here we are going to explain how we can use javascript to get the website url of the current page. Lets imagine our current page is http://myexample.com/example/index.html and we need to get each part of the url separately in javascript. We can access the parts of URL using javascript as follows.

window.location.protocol = "http"
window.location.host = " myexample.com "
window.location.pathname = "example/index.html"
window.location.href = "http://myexample.com/example/index.html"

If we want to access the entire website URL of the current page we can use window.location.href as mentioned above. Also we can access part by part of current page URL in javascript using above mentioned options. But there is no default option to get/access the query string values in current page URL in javascript.

 Redirect to another page using JavScript/ JavaScript redirect to a new page

 By using window.location we can redirect to another website page from javascript. So it’s better to use javascript navigation to redirect to another page instead of asp.net response.redirect function. Because Response.Redirect function first send to Server then identifying the web page that we need to navigate and again resend to client. There is a extra round trips will be happen in the case of Response.Redirect method. To avoid this round trips we can use Javascript redirection method as follows.

           window.location = “http://www.supershope.com”;

 How to access/get query string values in website URL using JavaScript (from client side)

 In order to access the querystring  of the current web page there are no default functions or methods in JavaScript.  To get URL parameters using javascript we are demonstrating a function that is used to get all the URL parameters from website in JavaScript.

 Javascript function to get all URL parameters from Website URL

 function getUrlParams() {
    var params = {};
    window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function (str, key, value) {
    params[key] = value;
});
    return params;
}

 var params = getUrlParams();

alert(params.id);

alert(params.name);

 Whenever we want to get querystring values from website URL we can call above function and which returns a object that holds all website URL parameters of the current page as key value pairs.

 How to get a particular querystring value from the URL in JavaScript

 We have noted that we cannot able to get query string from website URL in default methods in JavaScript. So we written a function to get all parameter values in the current page. But if we want to get a particular querystring values from the URL it’s difficult to use above method. That means if I want to get ‘id’ query string from the URL we need to get all parameters from the URL and from that collection get id values. Instead of this we are going to write another function which access the URL parameter name as the function parameter and return value of the particular querystring. If there is no query string in the URL with specified name will return empty string.

 Javascript function to get particular querystring value from the website URL

 function getParam(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
 
var id = getParam('id');
alert(id);
 

By using above function, we can easily get particular query string from client side script. If we send a value to above function and suppose there is no query string with that name, then it will return empty string. Hence we can easily accessing query string from client side using JavaScript.

Advertisements
  1. sindi pretty shange
    August 1, 2012 at 9:58 am

    My problem is that when trying to download my pictures on Facebook I am told that I must get a URL. Thanking u in advance

  2. August 1, 2012 at 10:23 am

    Hi sindi, Sorry, i didnt get your question. Can you please describe little more?

  3. November 4, 2012 at 6:35 am

    Will your get query string method work also to get data after the pound (#) sign in the URL?

  4. April 21, 2013 at 4:49 pm

    When I initially left a comment I appear to have clicked on the -Notify
    me when new comments are added- checkbox and from now on each time
    a comment is added I get 4 emails with the exact same comment.
    Perhaps there is a means you are able to remove
    me from that service? Cheers!

  5. seo
    May 2, 2013 at 12:52 pm

    First off I would like to say terrific blog! I had a quick question which I’d like to ask if you don’t mind.
    I was curious to find out how you center yourself
    and clear your mind before writing. I have had difficulty clearing my mind in getting my thoughts out there.
    I truly do enjoy writing however it just seems like
    the first 10 to 15 minutes are generally lost just trying to
    figure out how to begin. Any recommendations or hints?
    Cheers!

  6. May 3, 2013 at 11:00 am

    Hi seo,
    First of all thanks for your valuable comments and feedback :).
    Normally I have done a sample application in my pc with the example and from there I start blogging that feature here. Nothing more 🙂 🙂

    Again thanks for your precious visit. 🙂

  7. July 13, 2013 at 10:52 am

    whoah this weblog is excellent i like reading your posts. Keep up the great work!

    You recognize, lots of individuals are searching round for this information, you can aid them greatly.

  8. July 17, 2013 at 12:57 pm

    Good answer back in return of this issue
    with real arguments and telling the whole thing about that.

  9. July 19, 2013 at 7:48 pm

    I am in fact grateful to the owner of this web page who has shared this wonderful piece of writing at
    at this time.

  10. July 20, 2013 at 9:44 am

    continuously i used to read smaller posts which as well clear their motive,
    and that is also happening with this post which I am reading here.

  11. July 21, 2013 at 10:59 pm

    Hello my family member! I want to say that this article is
    amazing, great written and come with approximately all significant infos.
    I’d like to look more posts like this .

  12. Allie
    July 25, 2013 at 1:39 am

    When someone writes an post he/she maintains the image of
    a user in his/her brain that how a user can be aware
    of it. So that’s why this paragraph is amazing. Thanks!

  13. Chinabuye Coupon Code
    July 26, 2013 at 3:32 pm

    Thank you a bunch for sharing this with all of us you actually understand what you are speaking approximately!
    Bookmarked. Kindly also consult with my site =). We may have a link trade arrangement between us

  14. August 1, 2013 at 4:42 am

    Howdy, i read your blog from time to time and i own a similar one
    and i was just curious if you get a lot of spam remarks?
    If so how do you prevent it, any plugin or anything you can advise?
    I get so much lately it’s driving me mad so any assistance is very much appreciated.

  15. August 7, 2013 at 8:46 pm

    Hi! This is kind of off topic but I need some guidance from an established blog.

    Is it very difficult to set up your own blog? I’m not very techincal but I can figure things out pretty quick. I’m thinking about making my own but I’m not sure where to start. Do you have any tips or suggestions? Cheers

  16. October 18, 2013 at 12:06 am

    Great post.

  17. November 28, 2013 at 10:08 am

    louboutin shoes men a probably the greatest material .In truth man dolce and gabbana replica shoes
    Christian Louboutinshoes are artwork of employment, primarily Christian Louboutin pumps A handful of substantial-heeled boots can make any girl become more elegant and attractive particularly while you are departing to get a band These are typically currently available on an fantastic value! ! Only the one you have will your discover outstanding promotions on outstanding pieces such as this! Rush in now to go to this beautiful work of art These comfortable shoes will make you take a look extra sophisticated and increase your temperament Overseas christian louboutin stores are placed in the uk It really is unquestionable that Christian Christian louboutin has that inventive planning; for that reason, he manages to make his any shoes or boots layout special Christian Christian louboutin pumping systems, a sophisticated and beautiful girl Armani’s palm in developing the Erreuno selection can be another not too carefully guarded secret The assortment is made up at this moment of nonconfining outdoor jackets, shorts that are broad sufficient being named culottes as well as be taken wrongly for skirts, If you have a red one, is anodyne to say that shoes from Christian Louboutin The Louboutin’s hallmark lustrous reddish colored soles give an instantaneous stamp of manner superiority
    authentic . Around . Or if perhaps , this really is . They can be website is of one’s among the list of you could purchase it guarantee that a lot of of the cheap red bottom shoes this louboutin outlet wedges
    Arriving in gold metallic leather-based design and style actually makes this sandal modern browsing as well as the clear aspect over the entry too decorations this type of arrive Greater than 200 vogue contains arrange event booths in the fair, attracting some 5,000 potential buyers during the saturday and sunday He mementos reduce tee shirts and glowing colours, giving an original check out present-day garments Evening sweaters, chance with sparkle, were belted more than longer pleated chiffon skirts Christian Louboutinpumps and Christian Christian louboutin shoes both are in your own choices They really are particularly lovely if they get up on the honor level given that they make superb results She presented two smaller series: sportswear with nautical themes and silk or bed linen outfits Some women are classified as the enthusiasts of Christian Louboutin,unique crimson bottoms are so amazing and lumination that each and every young lady will adore it Global christian louboutin stores are located inside london Christian Louboutin not only famous for its top quality and fashionable style and also for its high pric, as the luxury brande No-one can dismiss the existence of Christian Louboutin inside the design environment You happen to be Hollywood star in others’ vision Now you get this opportunity to take part in Christian shoes and boots, you only need to pick the designer Christian louboutin sneakers you love Within his unique variety makes use of these components in light-weight cloth which include gabardine for working day and chiffon and organza for night time christian louboutin shoes uses some leathers in his own collection, treating them as if they were fabrics,

    Please visit it:jimmy choo short boots

  18. November 28, 2013 at 1:00 pm

    Pretty portion of content. I just stumbled upon your site and in accession
    capital to say that I get actually loved account your blog posts.
    Any way I’ll be subscribing to your feeds or even I achievement you get entry to constantly
    fast.

  19. June 12, 2015 at 2:17 am

    from Marine, thanks

  1. September 6, 2013 at 6:20 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: