简体   繁体   English

会话变量更改时重新加载iframe内容

[英]Reload iframe content when session variable changes

I'm making a webshop for a school project, and i've got an issue with the shopping cart. 我正在为学校项目制作网上商店,但购物车出现问题。 because the shoppingcart & webshop are in different iframes on the main page(got quite a few images on main page, don't want to reload that, thats why it's in iframes). 因为购物车和网上商店在首页上位于不同的iframe中(首页上有很多图像,所以不想重新加载,这就是为什么它在iframe中)。

Basically, my issue is that when something is selected in the webshop, the shopping cart doesn't update untill the entire webpage is reloaded. 基本上,我的问题是,当在网上商店中选择某项内容时,直到重新加载整个网页之后,购物车才会更新。 I'm storing shopping cart info in an array in a SESSION variable. 我将购物车信息存储在SESSION变量中的数组中。 basically, what i want is to reload the iframe of the cart when the session variable changes. 基本上,我想要的是在会话变量更改时重新加载购物车的iframe。

If the answer involves anything besides PHP/javascript, please give a clear explaination/example code, i've never used anything outside of PHP/js/html/css before. 如果答案除了PHP / javascript之外还涉及其他内容,请给出清晰的解释/示例代码,我以前从未使用过PHP / js / html / css之外的任何内容。 Thanks in advance :) been stuck on this for a few days now :S 在此先感谢:)几天了:S

创建一个javascript函数,以在用户向购物车中添加内容时触发以下代码:

document.getElementById('some_frame_id').contentWindow.location.reload();

我前面没有代码,但是我做了类似的事情,在父级中,您可以在“消息”上创建一个事件侦听器,然后在iframe中,您可以调用parent.postMessage,只要它们在同一域

Although APAD1's answer directly addresses what you asked, it sounds like your program has some deeper design problems. 尽管APAD1的答案直接针对您的要求,但听起来您的程序存在一些更深层的设计问题。 Using iframes for content from the same site (ie within one domain rather than content from a separate site) is bad practice. 将iframe用于来自同一网站的内容(例如,在一个域内而不是来自单独网站的内容)是不好的做法。

Consider instead displaying content in <div> s with unique ids, and using javascript to update the contents of those divs. 考虑改为在具有唯一ID的<div>显示内容,并使用javascript更新这些div的内容。 If you're using PHP on the back end, you can create a "page" that only generates the cart contents part of your page. 如果您在后端使用PHP,则可以创建一个“页面”,该页面仅生成页面中购物车内容的一部分。 Not only can you include it in each page without changes, but you can load it directly with javascript and insert the result into the current page to update it in place. 您不仅可以在没有任何更改的情况下将其包含在每个页面中,还可以直接使用javascript加载它,并将结果插入当前页面以对其进行适当更新。

Whenever your user adds an item to their cart, you can call the javascript method that refreshes the <div> with the cart's contents. 每当您的用户向购物车中添加商品时,您都可以调用javascript方法,用购物车的内容刷新<div>

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

相关问题 变量值变化时重新加载JS function - Reload JS function when variable value changes 单击按钮时重新加载iframe内容(本地HTML) - Reload iframe content (local HTML) when clicking buttons 在designMode中查找Iframe内容何时更改 - Finding when an Iframe's content changes when in designMode 如何在 iframe 内容更改时调整其高度? - How to resize the height of an iframe when its content changes? 内容更改时如何调整跨域iframe的大小? - How to resize a cross domain iframe when content changes? 当Iframe内容从按钮更改时,动态更改标头名称 - Dynamically change header names when Iframe content changes from a button 当 iframe 的内容动态变化时调整它的大小(例如:ajax 加载) - Resize iframe when its content dynamically changes (ex: ajax load) 在Chrome中,如何知道document.write何时更改i​​frame的内容? - In Chrome, how to know when document.write changes content of an iframe? 如果源页面更改,则自动重新加载iframe - Automatically reload an iframe if source page changes Firefox refresh button changes the src of the iFrame back to original as expected however the iFrame is still showing content from last session - Firefox refresh button changes the src of the iFrame back to original as expected however the iFrame is still showing content from last session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM