0
var url = window.location.href.toString();

the above line gives me the url of my current page correctly and my url is:

http://localhost/xyzCart/products.php?cat_id=35

However, using javascript how can i get only a portion of the url i.e. from the above url i just want

products.php?cat_id=35

How to accomplish this plz help.I have looked at similar questions in this forum but none were any help for me..

3
  • what parts of the url can we assume are static? will xyzCart always be the page? Commented Nov 5, 2015 at 13:35
  • yes, xyzCart would always be static Commented Nov 5, 2015 at 13:39
  • 1
    I don't think you've googled this enough. You can easily get URL parsing hits on it. Commented Nov 5, 2015 at 13:45

6 Answers 6

1

You can sliply use this:

var url = window.location.href.toString();
var newString = url.substr(url.lastIndexOf(".") + 1));

This will result in: php?cat_id=35 Good luck /Zorken17

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

1 Comment

Whay would it crash? If he wants the string after the last dot he can use this logik on any page.
0

You can use the location of the final /:

var page = url.substr(url.substr(0, (url + "?").indexOf("?")).lastIndexOf("/") + 1);

(This allows for / in a query string)

Comments

0

You can get your desired result by using javascript split() method.check this link for further detail

Comments

0

https://jsfiddle.net/x06ywtvo/

var urls = [
    "http://localhost/xyzCart/products.php?cat_id=35",
    "http://localhost/xyzCart/products.php",
    "http://www.google.com/xyzCart/products.php?cat_id=37"
];

var target = $('#target');
for(var i=0;i<urls.length;i++){
    var index = urls[i].indexOf("xyzCart");
    var sub = urls[i].substring(index, urls[i].length);
    target.append("<div>" + sub + "</div>");
}

Comments

0

Try the folowing javacript code to get the part you need. It splits up your url by the "/"s and takes the fourth part. This is superior to substr solutions in terms of descriptive clarity.

url.split("/")[4]

Or if url can contain more "/" path parts, then simply take the last split part.

var parts = url.split("/");
console.log( parts[parts.length-1] );

Comments

0

You will get all necessary values in window.location object. Kindly check on following CodePen Link for proper output.

I have added parameter test=1 Link: http://codepen.io/rajesh_dixit/pen/EVebJe?test=1

Code

(function() {
  var url = window.location.pathname.split('/');
  var index = 1;
  document.write("URL: ");
  document.write(window.location.href);
  document.write("<br/> Full Path: ");
  document.write(window.location.pathname);
  document.write("<br/> Last Value:")
  
  // For cases where '/' comes at the end 
  if(!url[url.length - index])
    index++;
  
  document.write(url[url.length-index])
  document.write("<br/> Query Parameter: ");
  document.write(window.location.search.substring(1));
})()

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.