jquery parse id number form url http://localhost:52717/ticketwizard/eventfeed/0/5868?&keywordsearchstring=eventkeyword in javascript

To parse the id number from the given URL using jQuery in JavaScript, you can use the following code:

index.tsx
// get the URL from the browser address bar
var url = window.location.href;
// split the URL using '/' character as separator
var url_parts = url.split('/');
// the id number may be in the third or fourth position of the URL depending on the structure
// check if the fourth part is a number
if (!isNaN(url_parts[3])) {
    var id_number = parseInt(url_parts[3]); // parse the number to integer
} else if (!isNaN(url_parts[4])) {
    var id_number = parseInt(url_parts[4]); // parse the number to integer
} else {
    var id_number = null; // unable to parse id number
}

console.log(id_number); // print the parsed id number to the console
641 chars
16 lines

This code first gets the URL from the browser address bar using window.location.href. It then splits the URL into an array of parts using '/' as the separator. The id number may be in the third or fourth position of the URL depending on the structure, so it checks first the fourth part and then the third part to see if it is a number. If it is a number, it is parsed to an integer using parseInt() and stored in the id_number variable. Finally, it prints the parsed id number to the console using console.log().

gistlibby LogSnag