简体   繁体   中英

Carrying over data from one html page to another using javascript

Hi i have a two page basic website with a submission box where you write down a name, then a second page that displays "Welcome___", however my website displays?Welcome=___, I know i need to parse a query string but I'm not sure how to

form.html

<body>
<form action="action.html" method="GET">
<input type="text" name="Welcome" />
<input type="submit" value="Submit" />
</form>
</body> 

action.html

<body>
<div id="write">
<p> Welcome  </p>
</div>
<script>
document.getElementById("write").innerHTML = window.location.search; 
</script>
</body>

To parse the querystring and extract from it the parameter you want the URLSearchParams interface offers useful methods to help you do what you want. A simple implementation might look like this.

<form action='action.html' method='GET'>
    <input type='text' name='Welcome' />
    <input type='submit' />
</form>


<div id='write'>
    <p>Welcome </p>
</div>

<script>
    let args=new URLSearchParams( location.search );
    if( args.has('Welcome') ){
        document.getElementById('write').querySelector('p').textContent += args.get('Welcome');
    }
</script>

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