简体   繁体   English

JavaScript:如何将其他信息传递到源页面?

[英]JavaScript: how to pass additional information to source page?

I have a website and when a user follows an internal link I would like to pass some extra information to a new page, so JavaScript on the destination page could do some useful highlighting. 我有一个网站,当用户单击内部链接时,我想将一些额外的信息传递到新页面,因此目标页面上的JavaScript可以做一些有用的突出显示。

There is an option to pass that information via the link parameters (GET), but it will generate lots of virtually duplicate pages and break pretty URLs concept. 有一个选项可以通过链接参数(GET)传递该信息,但是它将生成许多实际上重复的页面并破坏漂亮的URL概念。 Another way is to make a webapp using AJAX, but it will also bound content to a single URL. 另一种方法是使用AJAX创建Web应用程序,但它将内容也绑定到单个URL。

How can I transparently pass some information to the new page during navigation w/o messing with site's URL structure? 如何在导航过程中透明地将某些信息传递给新页面,而不会弄乱网站的URL结构?

您可以将数据存储在本地存储或会话存储中,然后在目标页面上再次检索它们。

So you have a few options. 因此,您有一些选择。

Form Submission 表格提交

First option post a form with the data. 第一种选择是将数据与表格一起过帐。 Add a hidden form, on the anchor click capture the click event, set the hidden fields with the values you want to send to the next page, and submit the form. 添加一个隐藏表单,在锚点单击上捕获click事件,为隐藏字段设置要发送到下一页的值,然后提交表单。 On the next page, read the post parameters in the backend and update the page. 在下一页上,阅读后端中的post参数并更新页面。

Local Storage 本地存储

On click of the anchor, set localStorage to the values you want to appear on the next page. 单击锚点后,将localStorage设置为要在下一页上显示的值。 When the next page loads, read the localStorage values and update the page. 当下一页加载时,读取localStorage值并更新页面。 Note: The server will not have access to the values 注意:服务器将无法访问这些值

Ajax with pushState Ajax与pushState

Use Ajax to submit the form. 使用Ajax提交表单。 When the Ajax call returns, use window.history.pushState to update the url with whatever url you want to be displayed to the user. 当Ajax调用返回时,使用window.history.pushState用您想要显示给用户的任何URL更新URL。

One of the options not mentioned is to create a dirty URL: 未提及的选项之一是创建 URL:

/destination/ param1 / value1 /... /目的/ 参数1 / 值1 / ...

then strip additional parameters at server-side and redirect: 然后在服务器端剥离其他参数并重定向:

/destination /目的地

keeping additional values stored at server-side (eg via sessions). 将其他值保存在服务器端(例如,通过会话)。 I still prefer using sessionStorage in a real application, but it worth mentioning anyway. 我仍然更喜欢在真实的应用程序中使用sessionStorage ,但是仍然值得一提。

What do you mean it will "bind content to a single url"? 您是什么意思,它将“将内容绑定到单个URL”? AJAX request is the first thing that comes to my mind as the solution to this problem. 作为解决此问题的方法,我想到的第一件事就是AJAX请求。 You dont have to use the url of the page to make the ajax request, you can build the url inside your javascript based on whatever conditions exist in your application. 您不必使用页面的url来发出ajax请求,您可以根据应用程序中存在的任何条件在JavaScript中构建url。

Besides AJAX and passing parameters in the URL, the only other thing I can think of is to use Cookies. 除了AJAX和在URL中传递参数外,我唯一想到的另一件事就是使用Cookies。 That of course runs into problems if the user has cookies disabled. 如果用户禁用了cookie,那当然会遇到问题。 I think an Ajax call to your server is the most robust way of handling the problem. 我认为对您的服务器进行Ajax调用是处理问题的最可靠方法。

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

相关问题 将其他信息传递到事件处理程序中 - Pass additional information into event handler 将信息从源站点传递到 JavaScript 应用程序链接的安全方法? - Secure way to pass information from source site to a JavaScript application link? 如何仅使用Javascript / jQuery将信息从查询字符串传递到Facebook中的页面选项卡应用程序 - How to pass information from a query string to a page tab app in Facebook using just Javascript/jQuery 如何将信息从JSF传递到javascript - How to pass information from JSF to javascript 如何将代码隐藏信息传递给javascript函数 - How to pass codebehind information into javascript function 如何将信息从php传递到javascript - how to pass information from php to javascript 如何使用JavaScript将输入表单值以及整个页面HTML源代码传递给后备bean? - How to get the input form values along with whole page HTML source using JavaScript to pass it to backing bean? 跨页面传递信息 - Pass information across page jsGrid:如何使用 ajax 将附加变量从 javascript 传递到 php - jsGrid: How to pass additional variables from javascript to php using ajax 如何在Javascript中将其他变量传递给Firebase promise的回调? - How to pass additional variables into a Firebase promise's callback in Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM