Javascript 将生成的id从一个页面传递到另一个页面
我真的很需要你的帮助。我已经和这些做了几天的斗争,我的项目失败了。我们将非常感谢你的帮助 我有3页 第一页接收我的数据,并创建html格式的版本。它是一个循环,返回10个帖子Javascript 将生成的id从一个页面传递到另一个页面,javascript,Javascript,我真的很需要你的帮助。我已经和这些做了几天的斗争,我的项目失败了。我们将非常感谢你的帮助 我有3页 第一页接收我的数据,并创建html格式的版本。它是一个循环,返回10个帖子 === 第2页是显示10篇文章的html页面 ==== 第3页。在第2页的文章只是特色图片和摘录和标题与网址。。。要阅读全文,请单击它并转到第3页 第3页使用每个帖子的唯一id显示完整帖子: 我的问题:如何将每个帖子id传递到第3页以查看完整内容 我试图将第1页中生成的id存储到localstorage,但bcos是
===
第2页是显示10篇文章的html页面
====
第3页。在第2页的文章只是特色图片和摘录和标题与网址。。。要阅读全文,请单击它并转到第3页
第3页使用每个帖子的唯一id显示完整帖子:
我的问题:如何将每个帖子id传递到第3页以查看完整内容
我试图将第1页中生成的id存储到localstorage,但bcos是一个循环。。。只存储最后一个。。
我的代码
第1页-脚本页接收数据
document.addEventListener("deviceready", onDeviceReady, false);
var portfolioPostsContainer = document.getElementById("portfolio-posts-container");
function onDeviceReady(){
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'http://myurl/wp-json/wp/v2/posts?_embed');
ourRequest.onload = function() {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
var data = JSON.parse(ourRequest.responseText);
createHTML(data);
console.log(data);
} else {
console.log("We connected to the server, but it returned an error.");
}
};
ourRequest.onerror = function() {
console.log("Connection error");
};
ourRequest.send();
}
Page 1 still: CreateHTMl create thru a loop
function createHTML(postsData) {
var ourHTMLString = '';
for (i = 0; i < 1; i++)
{
var posturl = postsData.link
ourHTMLString +='<tr>';
ourHTMLString += '<td>' + '<a href="loadpost.html" + rel="external" + " style="text-decoration:none ">' + postsData[i].title.rendered + '</a>'+'</td>';
ourHTMLString += '<td>' + '<a href="loadpost.html"><img width="100%" src ="' + postsData[i]._embedded['wp:featuredmedia']['0'].source_url + '" />' + '</a>'+'</td>';
ourHTMLString += '<td>' + postsData[i].excerpt.rendered + localStorage.setItem("postid",postsData[i].id)+'</td>';
//i tried to store each id in a localstorage but only the last one remains
ourHTMLString+= '</tr>';
} portfolioPostsContainer.innerHTML = ourHTMLString;
}
page two uses this to display ourHTMLString
<div id="portfolio-posts-container"></div>
page 3 Need each post id.
function onDeviceReady(){
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'http://myurl/wp-json/wp/v2/posts/'+mypostid+'?_embed=true')
ourRequest.onload = function() {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
var data = JSON.parse(ourRequest.responseText);
// createHTML(data); '+mypostid)
console.log(data);
var ourHTMLString = '';
Each post has its generated id from the api, how do i pass it to page 3 for displaying individual post
document.addEventListener(“DeviceRady”,OnDevicerady,false);
var portfoliopostcontainer=document.getElementById(“公文包发布容器”);
函数ondevicerady(){
var ourRequest=new XMLHttpRequest();
我们的请求。打开('GET','http://myurl/wp-json/wp/v2/posts?_embed');
ourRequest.onload=函数(){
如果(ourRequest.status>=200&&ourRequest.status<400){
var data=JSON.parse(ourRequest.responseText);
创建HTML(数据);
控制台日志(数据);
}否则{
log(“我们连接到了服务器,但它返回了一个错误。”);
}
};
ourRequest.onerror=函数(){
日志(“连接错误”);
};
ourRequest.send();
}
第1页:通过循环创建HTML
函数createHTML(postsData){
var ourHTMLString='';
对于(i=0;i<1;i++)
{
var postrl=postsData.link
我们的HTMLSTRING+='';
ourHTMLString+=''+'';
ourHTMLString+=''+'';
ourHTMLString+=''+postsData[i]。摘录.rendered+localStorage.setItem(“postid”,postsData[i].id)+'';
//我试图将每个id存储在本地存储中,但只剩下最后一个id
我们的HTMLSTRING+='';
}portfolioposcontainer.innerHTML=ourHTMLString;
}
第二页使用它来显示我们的HTMLSTRING
第3页需要每个帖子id。
函数ondevicerady(){
var ourRequest=new XMLHttpRequest();
我们的请求。打开('GET','http://myurl/wp-json/wp/v2/posts/“+mypostid+”?_embed=true')
ourRequest.onload=函数(){
如果(ourRequest.status>=200&&ourRequest.status<400){
var data=JSON.parse(ourRequest.responseText);
//创建HTML(数据);“+mypostid)
控制台日志(数据);
var ourHTMLString='';
每个帖子都有它从api生成的id,如何将其传递到第3页以显示单个帖子
尽管我对这个系统的整体结构有点困惑,但您可以将id作为查询字符串参数传递
<a href="page3.html?id=123">View post 123</a>
我认为你应该在你的后端用一个合适的API来处理这个问题。在客户端保存
id
并在其他页面中使用它是一个非常糟糕的方法,也是一个糟糕的编程解决方案。虽然它适用于移动应用程序。我如何在客户端保存它…我为它创建了localstorage,但只有最后生成的post id被存储在localstorage中…请非常感谢你的回复。这对我来说意义重大。谢谢。查看它。非常感谢。这真的为我做到了。我非常感谢你的时间。
var postMatch = /id=(\d+)/.exec(location.search);
if(postMatch) {
var postId = postMatch[1];
// Load post postId...
} else {
// No post was passed
}