单击按钮,使用参数重新加载窗口,然后删除按钮

问题描述:

因此,我有一个网页,其中包含整个页面,要求客户选择他们的首选货币。单击按钮,使用参数重新加载窗口,然后删除按钮

当他们选择美国或英国时,页面将使用URL中的货币参数重新加载(/?currency = GBP),这会更改页面上显示的价格。

但无论我尝试什么,我无法在页面重新加载时无法显示该div。

所以这里的弹出式在代码:

<div id="floatingBox"> 
    <div> 
     The two buttons go here. 
     <a href="/?currency=GBP">UK</a> 
     <a href="/?currency=USD">USA</a> 
    </div> 
</div> 

下面是该ID floatingBox风格:

#floatingBox { 
    z-index: 39879; 
    display: block; 
    position: fixed; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
} 

和这里的JavaScript的我使用的是:

if (window.location.search.search('GBP')) { 
    document.getElementById('floatingBox').display = 'none'; 
} 

if (window.location.search.search('USD')){ 
    document.getElementById('floatingBox').display = 'none'; 
} 

所以基本上一切工作正常,除了删除#floatingDiv当用户选择了一个选项。

当页面被重新加载并且存在URL参数时,如何让这个div不会出现?使用JavaScript而不是jQuery。

我想,这可能是最好使用的onclick和运行使用localStorage的功能,并呼吁该分区或类似的东西风格变量...

任何帮助表示赞赏:)

编辑

我要去测试:

var hide = localStorage.getItem('currChoice') || 0; 
if (hide == 1){ 
    document.getElementById('floatingBox').style.display = "none"; 
} 

function gbpClick(){ 
    var currChoice = 1; 
    localStorage.setItem('currChoice', currChoice); 
    location.href='/?currency=GBP'; 
} 

function usdClick(){ 
    var currChoice = 1; 
    localStorage.setItem('currChoice', currChoice); 
    location.href='/?currency=USD'; 
} 
+0

而不是在页面重新加载时隐藏它,为什么不默认隐藏它,并在重新加载期间决定是否需要显示它? –

我发布在主要问题上的编辑完美适用于我所需要的内容。

下面是最终代码: HTML:

<div id="floatingBox"> 
<div> 
The two buttons go here. 
<a href="/?currency=GBP">UK</a> 
<a href="/?currency=USD">USA</a> 
</div> 
</div> 

风格:

#floatingBox { 
    z-index: 39879; 
    display: block; 
    position: fixed; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
} 

的JavaScript:

var hide = localStorage.getItem('currChoice') || 0; 
if (hide == 1){ 
document.getElementById('floatingBox').style.display = "none"; 
localStorage.clear(); 
} 

function gbpClick(){ 
var currChoice = 1; 
localStorage.setItem('currChoice', currChoice); 
location.href='/?currency=GBP'; 
} 

function usdClick(){ 
var currChoice = 1; 
localStorage.setItem('currChoice', currChoice); 
location.href='/?currency=USD'; 
} 

编辑 新增localStorage.clear();到,如果您需要确保t如果用户将他们的URL从.com/?currency=GBP.com/?currency=USD更改回.com/,他们将会再次弹出。

手柄上点击前夕在重新加载页面之前关闭你的div然后重新加载之前,锚标记的nt。