How do i get the specific part of a url i jquery and javascript?

180
July 17, 2018, at 5:00 PM

I am checking out an order in wordpress via jquery ajax. Upon successfuly posting, wordpress returns a response to me with a result success value and url value.

I want to get the particular part of this url so I can use the id as an object for my purpose.

This is the structure of the url:
http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459

This is my current code:

  j('.my-checkout').on('submit', function(evt) {
    evt.preventDefault();
    var billing_customer_type = j("#billing_customer_type").val();
    // and so on...
    j.ajax({
      type: 'POST',
      url: 'http://localhost/mywebsite/ajax=checkout',
      cache: false,
      data: {
        'billing_customer_type':billing_customer_type,
        // and so on..
      },
      success: function (result) {
        var orderResponseUrl = result.redirect;

        j('.order-response-url').html(orderResponseUrl);
        // http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459
        orderResponseUrl.split("/");
        console.log(orderResponseUrl[3]);  

      },
      error: function(xhr,status,error) {
        console.log(error);
      },
      complete:function(){
      }
    });
 });

The result of my code above is just the the letter "p". I think because it started of the first letter of http and I used the index [3]

Do you know how can i get the specific part of the url that is 28564 ?

Answer 1

If the URL that you need to interact with is always the same, you can split the returned url at the 'order-received/' portion (which gives an array of everything before that, and everything after it).

Then rather than splitting again on the '?" which is another way of doing it - you can use parseFloat() to get the order number. This works since parseFloat returns all numerical values up to but not including the first non-numerical character ("?").

var urlStr = 'http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459'; 
 
var orderNumber = parseFloat(urlStr.split('order-received/')[1]); 
console.log(orderNumber); //gives 28564

Answer 2

Because when you do orderResponseUrl.split("/"); it does NOT change orderResponseUrl, it creates a new array.

var parts = orderResponseUrl.split("/");
console.log(parts);
Answer 3

if the length is always same then you can use substring function.

var str = "Hello world!";
var res = str.substring(1, 4);

now res contain

console.log(res); // ell

if you do not know the index, you can find like this.

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");

now n look like

console.log(n); // 13
READ ALSO
How to store Math expressions in a Mysql database using LaTeX

How to store Math expressions in a Mysql database using LaTeX

I am tasked with developing a math quiz system per topic for high school studentsI have questions and answers with different math formulae,expressions & symbols

109
MYSQL query with array that comes from ajax

MYSQL query with array that comes from ajax

I have an ''field_names' array that comes from ajax functionIts structure is simple

119
Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?

Android: How can I change AlertDialog Title Text Color and Background Color without using custom layout?

I want to change AlertDialog title color and background color without using custom layoutMy requirement,

1059