简体   繁体   中英

javascript variable in location.href

In javascript I have this variable:

var selectedItemId = null; // the actual value is set from another function

Then I have this function:

function editLink() {
    location.href = /*[[@{/supplycontracts/edit/}]]*/'';
}

Now I want to add the variable to the href, so that in the end it looks like this:

http://localhost:8080/myApp/supplycontracts/edit/?contractId=4

The number '4' at the end is the already substituted selectedItemId.

I tried stuff like this:

location.href = /*[[@{/supplycontracts/edit(${selectedItemId})}]]*/'';

But it didnt work.

If I do this:

location.href = /*[[@{/supplycontracts/edit(selectedItemId)}]]*/'';

I only get

http://localhost:8080/supply-app-0.0.1-SNAPSHOT/supplycontracts/edit?selectedItemId

How do I have to pass the selectedItemId to the href so I get the correct link?

For some reason, adding the parameter in the same line, as suggested by Nikola Lukic, did not work for me, although I have seen this notation in other sources as well. What finally worked is this:

function editLink() {
    var baseUrl = /*[[@{/supplycontracts/edit}]]*/'';
    location.href = baseUrl + "?contractId=" + selectedItemId;
}

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