user3150515
user3150515

Reputation:

How to get codeigniter URL parameter value using jQuery

my url is following:

http://localhost:8000/business/products/category/1

and i want to get the last 1 parameter from URL using jquery. Is there any method to do so?

Thanks in advance..

Upvotes: 0

Views: 1987

Answers (4)

mirza
mirza

Reputation: 5793

this will get last segment even with different number of segments:

$segs = $this->uri->segment_array();
echo end($segs);

Upvotes: 0

charlietfl
charlietfl

Reputation: 171690

Using split() creates an array, pop() returns last element of array

alert(location.pathname.split('/').pop())/// alerts 1

Upvotes: 2

Krishna38
Krishna38

Reputation: 735

You can achieve this by splitting the URL into segment first.

Try like this

var segments = url.split( '/' );
var action = segments[3];
var id = segments[4];

You get the URL using Jquery like this

$(location).attr('href');

Upvotes: 0

Paul Zepernick
Paul Zepernick

Reputation: 1462

You can either do this by getting it from php and embedding it into the javascript:

var category = <?php echo $this -> uri -> uri_to_assoc()['category'] ?>;

OR using only javascript

var url = window.location.href;
var idx = url.indexOf("category");
var category = url.substring(idx).split("/")[1];

Upvotes: 0

Related Questions