How to get parameter from url?

I have such a URL. http: // localhost: 8080 / steer / trip / create / 3 . where on my page I want to get the value "3" using jquery. Please help me

+3
source share
6 answers

C: Highlight a button based on location

var pathname = window.location.pathname.split("/");
var filename = pathname[pathname.length-1];

alert(filename);
+12
source

This may be redundant, but for parsing the URL, I found this useful:

The jQuery plugin is for parsing URLs and provides easy access to information inside them, such as protocol, host, port, path segments, and various query string values.

In your case, it comes down to something like:

jQuery.url.setUrl("http://localhost:8080/steer/trip/create/3").segment("4");
+2

js

var arr = (window.location.pathname).split("/");
var val = (arr[arr.length-1]);

, , val

+2

"3" URL- "/" .

var urlsegment = top.location.href.match(/([^\/]+)$/)[1]
+1

, jQuery .

jQuery, URL ( , ).

ASP.NET MVC, Route, : {controller} {action} {param}, http:\myhost\mycontroller\myaction\3 http:\myhost\mycontroller\myaction? param = 3 , URL .

Examples in the jQuery mdatadata plugin are useful.

Below is one example in the link above.

<li class="someclass {some: 'data'} anotherclass">...</li>
<script>alert($('li.someclass').metadata().some);</script>

Please ignore this answer if my assumption is wrong.

0
source

Here you can easily read and understand the JS function, which you can use to extract any variable from the URL.

All you have to do is call this function with the name of the variable you want to filter, and it will return you a value.

Hope this helps:

function getVarFromURL(varName){
            var url = window.location.href;
            url = url.substring(url.indexOf('?'));
            var urlLowerCase = url.toLowerCase();
            varName = varName.toLowerCase();
            if (urlLowerCase.indexOf(varName + "=") != -1) {
                var value = url.substring(urlLowerCase.indexOf(varName) + varName.length + 1);
                if (value.indexOf('&') != -1) {
                    value = value.substring(0, value.indexOf('&'));
                }
                return value;
            }
            else {
                return null;
            }
}
-2
source

All Articles