Showing posts with label URL. Show all posts
Showing posts with label URL. Show all posts

How to read query string value in javascript

In this article am going to explain how to read query string value using JavaScript.

Use the following function to read query string value from url. You just need to pass the parameter name to this function.

Method 1:

var getQueryString = function ( field) {
    var href = window.location.href;
    var reg = new RegExp( '[?&]' + field + '=([^&#]*)', 'i' );
    var string = reg.exec(href);
    return string ? string[1] : null;
};

Method 2 :

You can also use the following javascript function to read querystring value.
function getQueryString(param) {
var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < url.length; i++) {
var urlparam = url[i].split('=');
if (urlparam[0] == param) {
return urlparam[1];
}
}
}

For example assume url as http://mysite.com?myParam1=test1&myParam2=test2

Now to read the query string value from above url use the following javascript code.

var myParam1 = getQueryString('myParam1'); // returns 'test1'
var myParam2 = getQueryString('myParam2'); // returns 'test2'
var testParam = getQueryString('something'); // returns null

Note: If a parameter is present several times (?param=test1&param=test2), you will get the first value


In this way we can read query string value from url in JavaScript


For more posts on javascript visit: JavaScript


Read more...

get current window url in javascript

You can get the url of the current window using javascript.

To get the current window url, use the below javascript:

var url = window.location.href;
The above javascript code gives the full url of the window.


To get the current window url using jquery, use the below code:

var url = $(location).attr('href');


To get the origin of the url use the following javascript code:


var origin = window.location.origin;


To get the pathname use:


var pathname = window.location.pathname;


To get the current window protocol use:


var origin = window.location.protocol;


To get the port number of the current window use:


var port = window.location.port;


To Reload the current page using javascript use:


window.location.reload();


In this way you can get the url of the current window using javascript.

For more posts regaring please visit : Javascript


Read more...

What is Fragment in URL


The part after the # sign is known as the fragment. The fragment is different than the other pieces we've looked at so far, because unlike the URL path and query string, the fragment is not processed by the server. The fragment is only used on the client and it identifies a particular section of a resource. Specifically, the fragment is typically used to identify a specific HTML element in a page by the element's ID.

Web browsers will typically align the initial display of a webpage such that the top of the element identified by the fragment is at the top of the screen. As an example, the URL http://www.coding-issues.blogspot.com/Asp.Net#feedback has the fragment value "feedback". If you follow the URL, your web browser should scroll down the page to show the feedback section of a particular blog post on my blog. 

Your browser retrieved the entire resource (the blog post), but focused your attention to a specific area—the feedback section. You can imagine the HTML for the blog post looking like the following (with all the text content omitted):

<div id="post">
...
</div>

<div id="feedback">
...
</div>

The client makes sure the element with the “feedback” ID is at the top.

Read more...