joe
joe

Reputation: 1135

How to append string to the url and then make url show the string?

Javascript = looked through Google and Stack Overflow -I am still lost as I am still new in programming and everything else. I have some questions that I need to know

so far, I have this below:

var getUrladdress = window.location.href; //get current url address var sortbydiscount = "?sortby"; var discount = ["20","30","50","70"];

What I wanted to know how to do is:

  1. append string from discount array (I have tried to use append() but it doesn't work :(
  2. then compare the discount array against percentDiscount (still being worked on in other question)
  3. sort the items (final one) to display all items under 20%, 30% etc group.

Could you please give me insight/guide me on how to append string from discount array and then have the url show that "http://www.xxx.com?sortby30" in live address bar? Then could it be possible to compare array against percentDiscount?

Many thank yous in advance.

EDITED:: #3 - I wanted to have sale items categorized in different discount group: i.e. if 20%, then all items which are on sale for 20% should be only displayed on the page.

Upvotes: 0

Views: 1465

Answers (1)

PleaseStand
PleaseStand

Reputation: 32052

You really should get a good book on JavaScript and learn from that if you are so unsure about core language features. That said:

  1. You are confusing appending an HTML element using jQuery (just one of the web application libraries or "frameworks" available for JavaScript) with appending one string to another (concatenating strings). The plus operator can be used to concatenate strings:

    getUrladdress = getUrladdress + sortbydiscount;
    
  2. To check to see if a particular item is in an array, you can use the .indexOf method of the array. Because the method was added only in ES5 (the latest version of the ECMAScript/JavaScript specification), the documentation I have linked to includes contains some compatibility code to use to get this to work on Internet Explorer.

    if(discount.indexOf(percentdiscount) != -1) {
        // found
    } else {
        // not found
    }
    
  3. I'm not sure what you mean by that one; perhaps you can clarify exactly what you are trying to do? Show a list of items under each discount percentage on the web page?

Upvotes: 1

Related Questions