0

I am attempting to reload the current page by passing parameters to javascript and calling location.href.

function setEventListener(item) {
    item.addEventListener('click', function () {

        let sortColumn = item.parentElement.getAttribute('data-sortColumn');
        let sortOrder = item.getAttribute('data-sortOrder');
        let controller = item.getAttribute('data-controller')

        let hostName = window.location.hostname + ':' + window.location.port;
        let url = new URL(hostName + controller + sortColumn + '/' + sortOrder + '/');
        location.href = url.href;
    }

The url is good, I can grab it from console output and paste in to the window and load the page fine. But this just calls the click function and does nothing.

1 Answer 1

1

Maybe try to add the transfer protocol

item.addEventListener("click", function() {
    let sortColumn = item.parentElement.getAttribute("data-sortColumn");
    let sortOrder = item.getAttribute("data-sortOrder");
    let controller = item.getAttribute("data-controller");

    let hostName = window.location.hostname + ":" + window.location.port;
    let url = new URL(
      location.protocol + "//" + hostName + "/" + controller + sortColumn + "/" + sortOrder + "/"
    );
    location.href = url.href;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I thought it might default that for some reason, now I just need to figure out why it duplicates the attributes in the url on subsequent clicks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.