Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

javascript - 将数据从一个HTML文件传输到另一个HTML文件(Transfer data from one HTML file to another)

I'm new to HTML and Javascript, what i'm trying to do is from an HTML file i want to extract the things that set there and display it to another HTML file through Javascript(我是HTML和Java脚本的新手,我想做的是从an HTML文件中提取要设置的内容,并通过Javascript将其显示到another HTML文件中。)

Here's what i done so far to test it:(这是我到目前为止所做的测试:)
(testing.html)((testing.html)) <html> <head> <script language="javascript" type="text/javascript" src="asd.js"></script> </head> <body> <form name="form1" action="next.html" method="get"> name:<input type ="text" id="name" name="n"> <input type="submit" value="next" > <button type="button" id="print" onClick="testJS()"> Print </button> </form> </body> </html> (next.html)((next.html)) <head> <script language="javascript" type="text/javascript" src="asd.js"></script> </head> <body> <form name="form1" action="next.html" method="get"> <table> <tr> <td id="here">test</td> </tr> </table> </form> </body> </html> (asd.js)((asd.js)) function testJS() { var b = document.getElementById('name').value document.getElementById('here').innerHTML = b; } test.html -> ads.js (will extract value from the test.html and set to next.html) -> next.html(test.html > ads.js (将从test.html中提取值并设置为next.html)-> next.html)   ask by newbie translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this code: In testing.html(试试这个代码:在testing.html中)

function testJS() { var b = document.getElementById('name').value, url = 'http://path_to_your_html_files/next.html?name=' + encodeURIComponent(b); document.location.href = url; } And in next.html:(在next.html中:) window.onload = function () { var url = document.location.href, params = url.split('?')[1].split('&'), data = {}, tmp; for (var i = 0, l = params.length; i < l; i++) { tmp = params[i].split('='); data[tmp[0]] = tmp[1]; } document.getElementById('here').innerHTML = data.name; } Description: javascript can't share data between different pages, and we must to use some solutions, eg URL get params (in my code i used this way), cookies, localStorage, etc. Store the name parameter in URL (?name=...) and in next.html parse URL and get all params from prev page.(说明:javascript无法在不同页面之间共享数据,我们必须使用一些解决方案,例如URL get params(以这种方式使用我的代码),cookie,localStorage等。将name参数存储在URL中(?name = ...),然后在next.html中解析URL并从上一页获取所有参数。) PS.(PS。) i'm an non-native english speaker, will you please correct my message, if necessary(我不是英语母语人士,请根据需要更正我的信息)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...