简体   繁体   English

如何创建包含动态html的可共享网址(javascript)

[英]How to create a sharable url containing dynamic html (javascript)

What is the best practice to create unique shareable urls for some text lists users create? 为用户创建的某些文本列表创建唯一的可共享URL的最佳实践是什么?

It's a single page website with a content div where users create text lists. 这是一个具有内容div的单页网站,用户可以在其中创建文本列表。 Once they click share, how can I store those values inside a shareable url so that another user going to that address loads the same list? 当他们单击共享时,如何将这些值存储在可共享的url中,以便另一个访问该地址的用户加载相同的列表?

I'm using html, js, jquery, php. 我正在使用html,js,jquery,php。

EDIT: as suggested below i'm already saving the lists on a database (firebase), and each have an unique ID, so I'd need to understand how I can create urls with a list id in it, and how to read the url back. 编辑:如下所示,我已经将列表保存在数据库(firebase)上,并且每个列表都有唯一的ID,因此我需要了解如何创建带有列表ID的网址,以及如何读取网址返回。

EDIT 2: so this is the code i'm using right now, combining answers from marzelin and the Alchemist Shahed in my other question about my database structure ( Firebase how to find child knowing its id but not its parent's id (js) ): 编辑2:这是我现在正在使用的代码,结合了marzelin炼金术师Shahed在我有关数据库结构的其他问题中的答案( Firebase如何找到知道其ID但不知道其父母ID(js)的孩子 ):

//js inside window load function:
  const keyOfDynamicHtmlItemRef = new URL(window.location).searchParams.get("share")
  if (keyOfDynamicHtmlItemRef) {
    var dynamicHtmlListRef = firebase.database().ref('users');
    // var dynamicHtmlItemRef = dynamicHtmlListRef.child(keyOfDynamicHtmlItemRef);
    // console.log(keyOfDynamicHtmlItemRef);
    // dynamicHtmlItemRef.once("value").then(dynamicHtmlSnap => {
    //   texta.innerHTML = dynamicHtmlSnap.val();
    // });
    dynamicHtmlListRef.once('value').then((snapshot)=>{
     snapshot.forEach(function(data) {
        if (data.key == keyOfDynamicHtmlItemRef) {
          myVar = data.c;
          myContentDiv.innerHTML = myVar;
        }
     });
    });
  }

and i'm simply trying to manually write the url in the searchbar as a first step, as https://example.com/?share=<random list id i copied from db> , but it does nothing. 并且我只是尝试第一步,将网址手动写在搜索https://example.com/?share=<random list id i copied from db> ,如https://example.com/?share=<random list id i copied from db> ,但它什么也没做。

So the way I would to this is I would have the users share click trigger a save to database saving all the dynamically generated content into a table. 因此,我要这样做的方式是让用户共享点击触发存储到数据库的保存,将所有动态生成的内容保存到表中。

One of the table values would be a randomly generated unique identifier of some sort that I would use as a query in the url like https://www.example.org/?share=skd822475 表值之一是随机生成的某种唯一标识符,我可以将其用作网址中的查询,例如https://www.example.org/?share=skd822475

Then when a user visits the site and that query is in the url id use the unique identifier to look up the database and publish the dynamic content back on the page. 然后,当用户访问该站点并且该查询位于url id中时,使用唯一标识符来查找数据库并将动态内容发布回页面上。

I would also put a half life on the database entry's of say no more than 30 days so that it doesn't clog up the db. 我还将在数据库条目上设置半衰期,例如不超过30天,以免阻塞数据库。

Let's start with the first question " How to create urls with a list id in it? " 让我们从第一个问题开始:“ 如何创建带有列表ID的URL?

The thing is that to answer this one we need to answer the second question first witch is " How to read the url back? " 问题是要回答这个问题,我们首先要回答第二个问题:“ 如何读回URL?

Consider that you have a php page named "draft". 考虑您有一个名为“草稿”的php页面。 when a user visit https://www.example.com/draft?listId=an_id you will get listId using php like so $_GET("listId") and use that value to retrieve the list data and display the page content. 当用户访问https://www.example.com/draft?listId=an_id您将像$_GET("listId")这样使用php获取listId并使用该值检索列表数据并显示页面内容。

Now coming back to the first question, if the user share the draft like in social media (ex: facebook) then there is no problem because he will share a link and all his followers and any other user can access it easily. 现在回到第一个问题,如果用户像在社交媒体(例如:facebook)中一样共享草稿,则没有问题,因为他将共享一个链接,并且所有关注者和任何其他用户都可以轻松访问它。 but if the user just save the draft then you will have to change the page url dynamically like this window.history.pushState(null, null, '/draft?listId=your_newly_created_id'); 但是,如果用户只是保存草稿,则必须像此window.history.pushState(null, null, '/draft?listId=your_newly_created_id');一样动态地更改页面网址window.history.pushState(null, null, '/draft?listId=your_newly_created_id'); and so the user will copy the url and do whatever he wnt with it (sharing it in stackoverflow maybe example using jsfiddle http://jsfiddle.net/F2es9/ (you can change the url to look like this using 'htaccess' file)) at the end I would like to tell you that we don't "create" urls. 因此用户将复制该网址并进行任何操作(在stackoverflow中共享该网址,例如,使用jsfiddle http://jsfiddle.net/F2es9/的示例(您可以使用“ htaccess”文件将其更改为如下所示的网址) )最后,我想告诉您,我们不会“创建”网址。

Edit 编辑

without using php code (or any other server side code). 而不使用php代码(或任何其他服务器端代码)。 the difference will be in retrieving the data. 区别在于检索数据。 instead of using $_GET("listId") you will use new URL(window.location).searchParams.get("listId") to get the list id in javascript then using this value you can retrieve data from firebase and display your content 而不是使用$_GET("listId")您将使用new URL(window.location).searchParams.get("listId")在javascript中获取列表ID,然后使用此值可以从firebase检索数据并显示内容

Saving data and creating shareable link: 保存数据并创建共享链接:

document.querySelector(".share").addEventListener("click" => {
  var dynamicHtmlListRef = firebase.database().ref('dynamic_html');
  var dynamicHtmlItemRef = dynamicHtmlListRef.push();
  dynamicHtmlItemRef.set(userCreatedDynamicHtml);
  var keyOfDynamicHtmlItem = dynamicHtmlItemRef.key;
  var linkToDynamicHtmlItem = `${window.location}?share=${keyofDynamicHtmlItem}`;
  alert(`link: ${linkToDynamicHtmlItem}`)
})

Showing the dynamic HTML based on query parameters: 显示基于查询参数的动态HTML:

const keyOfDynamicHtmlItemRef = new URL(window.location).searchParams.get("share")
if (keyOfDynamicHtmlItemRef) {
  var dynamicHtmlListRef = firebase.database().ref('dynamic_html');
  var dynamicHtmlItemRef = dynamicHtmlListRef.child(keyOfDynamicHtmlItemRef);
  keyOfDynamicHtmlItemRef.once("value").then(dynamicHtmlSnap => {
    document.querySelector(".dynamic-html-mountpoint").innerHTML = dynamicHtmlSnap.val();
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM