How to parse URL using javascript

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to parse URL using javascript

In certain cases you may want to parse a URL using javascript, although I don’t specifically recommend this, rather you should use a php approach, server side. Javascript can be manipulated and if passing the URL somewhere and then actually doing work with the result, you may have surprises in the end …

But for static functions that you just need to show the result in browser, it should be safe enough.

Scenario

I used a static variable for this example as we just want to show you the actual parsing of this url.

Solution

The solution is to use the split function from javascript.

Output

Once you have this array, you can start and use the elements in your little script.

Combine elements in a javascript array

To combine the elements of the array, you would need to use the the join function from javascript:

The output will be:

Again, rather then using this in a ajax call for instance, better send the full url and parse the url sent in php, it is safer like that!

 

Request an article ←