0

I have some jQuery/JavaScript that I use for language selection which will get the URL and then append the relevant language selection syntax to the end of it. Please see below:

jQuery/JavaScript:

jQuery(document).ready(function() {
    var url = jQuery(location).attr('href') + '?___store=default&___from_store=default';
    jQuery(".english").attr("href", (url));
});

HTML:

<div class="languageselection">
    <table>
        <tr>
          <td><a class="english">English</a></td>
        </tr>
    </table>
</div>

This works great and does what I need it to, unless a user was to select a language and then try to move back to another language. This is because the ?___store=default&___from_store=default is added twice onto the URL. Resulting in the following URL:

www.example.com/product.html?___store=default&___from_store=default?___store=default&___from_store=default

So my query is whether it's possible to amend my jQuery variable so that it only gets the URL up to the .html point of the URL? This will result in the language syntax only ever being added once.

Thank you for any help.

rrk
  • 15,677
  • 4
  • 29
  • 45

2 Answers2

1

You can use window.location.hostname + window.location.pathname to get what you want. See Location object : MDN

Gokhan Kurt
  • 8,239
  • 1
  • 27
  • 51
0

If your need is to get current url. Try this

 $(document).ready(function() {
//jquery
$(location).attr('href');

//pure javascript
var pathname = window.location.pathname;

// to show it in an alert window
alert(window.location);
});

There is also this function that may help when determining absolute paths.

function getAbsolutePath() {
var loc = window.location;
var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));

}

naveenkumar.s
  • 901
  • 8
  • 17