JavaScript-在同一窗口/选项卡中打开链接

时间:2018-06-26 12:36:21

标签: javascript cookies

下面是“ Cookie Consent”中有关带有链接的cookie标语的一段代码。问题:代码在新标签页而不是同一标签页/窗口中打开链接-请参见代码底部。要进行这项工作,需要添加/更改什么?同样,window.open("https://www.youraddress.com","_self")似乎无法解决问题。 Edit2:按建议更改“元素”仍然无法产生正确的结果...

    <link rel="stylesheet" type="text/css" 
 href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js"></script>
<script>
window.addEventListener("load", function(){
window.cookieconsent.initialise({
  "palette": {
    "popup": {
      "background": "#ebebeb",
      "text": "#404040"
    },
    "button": {
      "background": "#ff0099",
      "text": "#ffffff"
    }
  },

  "elements": { 
  "link": '<a aria-label="learn more about cookies" tabindex="0" class="cc-link" href="{{href}}" target="_self">{{link}}</a>',
},
  "content": {
    "href": "https://www.example.com/privacy-policy" 
  }

})});
</script>

3 个答案:

答案 0 :(得分:0)

要在JavaScript的同一标签中打开链接,请使用代码:-

window.location = 'http://www.google.com' ;

检查此链接Your Example可以正常使用。

答案 1 :(得分:0)

好的,这是有关在同一窗口中打开“了解更多信息”链接的“ Cookieconsent”的工作代码:

<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.js"></script>
<script>
window.addEventListener("load", function(){
window.cookieconsent.initialise({
  "palette": {
    "popup": {
      "background": "#ebebeb",
      "text": "#404040"
    },
    "button": {
      "background": "#ff0099",
      "text": "#ffffff"
    }
  },

  "elements": {
      'messagelink': '<span id="cookieconsent:desc" class="cc-message">\{\{message\}\} <a aria-label="learn more about cookies" tabindex="0" class="cc-link" href="\{\{href\}\}" target="_self">\{\{link\}\}</a></span>',
      'link': '<a aria-label="learn more about cookies" tabindex="0" class="cc-link" href="\{\{href\}\}" target="_self">\{\{link\}\}</a>'
                        },
  "content": {
    "href": "https://www.example.com/learn-more" 
  }

})});
</script>

答案 2 :(得分:0)

要在JavaScript的同一标签中打开链接,请使用以下代码: window.open(“ https://www.youraddress.com”,“ _ self”);