简体   繁体   中英

javascript window.location.href keeps adding to url

I have a simple dropdown that forwards to a new url onChange.

It looks like this:

function changeCompanyType(companyType) {
      window.location.href = 'type/'+companyType+'/';

    }

The first change works great and goes to the url:

http://127.0.0.1/companies/type/bank/

The next time I click on the dropdown from the new page it adds type and the company type again:

http://127.0.0.1/companies/type/bank/type/hosptial/

How can I just have the company type changed so the url doesn't keep being added to?

Prefix with a forward slash ( / )

window.location.href = '/type/'+companyType+'/';
                        ^ -- a slash to make an absolute url

// or (depends on what you want to do)
window.location.href = '/companies/type/'+companyType+'/';
                        ^ -- a slash to make an absolute url

Add a '/' at the front of the link like this

function changeCompanyType(companyType) {
  window.location.href = '/type/'+companyType+'/';
}

Use:

function changeCompanyType(companyType) {
    window.location.href = '/type/'+companyType+'/';

}

Add line:

companyType = '';

after you have set the href.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM