简体   繁体   中英

Passing parameters between html pages using jquery

I have one html page which contains a jquery function.

<script>
function loadCustomers() {
    $.ajax({
        type: 'GET',
        url: 'http://localhost:8080/cache/getCustomers',
        dataType: 'json',
        success: function(data) {
            var rows = [];
            $.each(data,function(id,value) {
                rows.push('<tr><td><a href="clientSiteInfo.html?client=">'+id+'</td><td>'+value+'</td></tr>');
            });
            $('table').append(rows.join(''));
        }
    });
};
window.onload = loadCustomers;
</script>

I have linked another html page for each row. When each rows populated, the id values has to be passed to the clientSiteInfo.html page.

In the clientSiteInfo.html page i have another jquery function similar to above.

<script>
function loadSites() {
    $.ajax({
        type: 'GET',
        url: 'http://localhost:8080/cache/getSite?clientName='+${param.client},
        dataType: 'json',
        success: function(data) {
            var rows = [];
            $.each(data,function(id,value) {
                rows.push('<tr><td>'+id+'</td><td>'+value.machine+'</td><td>'+value.state+'</td></tr>');
            });
            $('table').append(rows.join(''));
        }
    });
};
window.onload = loadSites;
</script>

in the GET url I try to read client parameter. But it is not passing from my initial page.

What Im doing wrong here? I look for simple solution

jQuery doesn't have a native way to read the url parameters. However, javascript works just fine:

function getParameterByName(name) {
  const match = RegExp(`[?&]${name}=([^&]*)`).exec(window.location.search);
  return match && decodeURIComponent(match[1].replace(/\+/g, ' ') );
}

In your code you would just call it as getParameterByName('client')

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