0

Bellow is my URL and [3] is my to address, It differs when user enters different location through form, I just want first word, that is mysore ,how can i remove other things?

skc1/final.html?date=12-04-
2017&time=2&from=Bangalore%2C+Karnataka%2C+India&to=Mysore+division%2C+
Karnataka%2C+India&distance=150+km

I am using this ,but it should be changed everytime if i enter different location.

window.dest_value = dest_value.replace("%2C+Karnataka%2C+India","");

4 Answers 4

1

You can use decodeURIComponent() to decode them:

var url = "skc1/final.html?date=12-04-2017&time=2&from=Bangalore%2C+Karnataka%2C+India&to=Mysore+division%2C+Karnataka%2C+India&distance=150+km";

url = decodeURIComponent(url);

document.body.textContent = url;

Sign up to request clarification or add additional context in comments.

Comments

0

I just want first word, That is mysore ,how can i remove other things?

You can use String.prototype.match() with RegExp /(to=)(\w+)(?=\+)/ to match "to=" followed by word characters followed by "+" character, call .pop() on resulting array

var url = "skc1/final.html?date=12-04-2017&time=2&from=Bangalore%2C+Karnataka%2C+India&to=Mysore+division%2C+Karnataka%2C+India&distance=150+km";
var res = url.match(/(to=)(\w+)(?=\+)/).pop();

console.log(res);

1 Comment

@Jai They cannot help themselves.
0

I got this way to fetch the current URL on this site with the following question:

Get current URL in JavaScript?

var pathname = window.location.pathname; // Returns path only
var url      = window.location.href;     // Returns full URL

Maybe it will help you.

Comments

0

You can also get the querystring values separately as -

window.onload
{
	var url = "skc1/final.html?date=12-04 -2017&time=2&from=Bangalore%2C+Karnataka%2C+India&to=Mysore+division%2C+Karnataka%2C+India&distance=150+km";
    var vars = [];
    var hash;
   
    var hashes = url.slice(url.indexOf('?') + 1).split('&');
     console.log(hashes);
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.