English 中文(简体)
我的文字在“ Chrome”上,但不是在“Safari”上正确工作。
原标题:My script works properly on Chrome, but not on Safari

下面的文字是我的网站上的黑暗节奏守则。

我没有真正了解贾瓦特(我只知道沙里),因此,起草这一法典是一项挑战,可能写不好。 但是,用Django Templating语言对“is_dark_mode/code>”的会期变量进行核对,以检查目前用户是否将自己的观点置于轻度或黑暗的状态,然后着手为这种变数转换波兰,并增加或删除“dark-mode”类别,进入我的超文本体(在黑暗的环境下,我是如何改变风格的)。

该公司在“ Chrome”进行罚款,但该公司根本不停在Sari,我不知道为什么。 在使用 Java典时,它犯了一个错误:

SyntaxError: Unexpected token  ; 

“SyntaxError”指的是这一法典:

var darkMode = ;

难道有人会给我一手把这一点混为一谈吗? 我的学习只是背后,因此,我对如何解决这一问题没有想法,我已经尝试了,但没有成功。

<script>
      document.addEventListener("DOMContentLoaded", function () {
        var modeToggle = document.getElementById("modeToggle");

        function setInitialDarkMode() {
          var darkMode = {{ request.session.is_dark_mode|default_if_none:"false"|lower }};
          modeToggle.checked = darkMode;

          if (darkMode) {
            document.body.classList.add("dark-mode");
          }
        }

        modeToggle.addEventListener("change", function () {
          var darkMode = modeToggle.checked;

          fetch("{% url  toggle-mode  %}", {
            method:  POST ,
            headers: {
               X-CSRFToken :  {{ csrf_token }} ,
               Content-Type :  application/json ,
            },
            body: JSON.stringify({ dark_mode: darkMode }),
          })
            .then(response => response.json())
            .then(data => {
              if (darkMode) {
                document.body.classList.add("dark-mode");
              } else {
                document.body.classList.remove("dark-mode");
              }
              console.log(data);
            })
            .catch(error => console.error(error));
        });

        setInitialDarkMode();

      });
</script>
最佳回答

采取上述同样做法的另一个方式

function checkSessionVariable() {
  var darkMode = "{{ request.session.is_dark_mode|default_if_none:"false"|lower|default:"false" }}";
  if (darkMode === "true") {
    // for calling setInitialDarkMode() or setting the toggle to true
    setInitialDarkMode();
  }
}

document.addEventListener("DOMContentLoaded", function() {
  var modeToggle = document.getElementById("modeToggle");
  
  // Calling checkSessionVariable() function on page load
  checkSessionVariable();
  
  // put all other code

});
问题回答

It looks like the issue in your code is related to how you are initializing the darkMode variable. The error you are seeing, "SyntaxError: Unexpected token ; ", is because you have this line:

var darkMode = ;

变式<代码>darkMode正在启动,没有价值,造成yn误。 为了解决这个问题,你应当删除以下两条:

var darkMode = ;

相反,你应当利用你的Django模板的价值,在<条码>内建立<条码>darkMode。 你的法典应当这样考虑:

function setInitialDarkMode() {
  var darkMode = {{ request.session.is_dark_mode|default_if_none:"false"|lower }};
  modeToggle.checked = darkMode;

  if (darkMode) {
    document.body.classList.add("dark-mode");
  }
}

该代码根据您的詹戈模板的价值,正确地将<代码>darkMode变量初步化。 • 确保您在Django模板变量中不包含任何外在性质或缺失的数值:{quest.session.is_dark_modef_if_none:”false ́s ́sault ,因为它应为本守则提供有效的 Java本毛价值(truefalse)。

Once you make this change, your code should work as expected in Safari and other browsers.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.