1

首先,如果这是重复的,让我道歉,但我的搜索都没有让我比现在更开明。也就是说,我目前正在尝试使用 JavaScript/Ajax/jQuery,但遇到了一点问题。

我想在我的主页上包含一个功能检查,用户可以在其中测试他们的浏览器是否配置为允许 JavaScript 和 cookie(如果用户的浏览器不允许 JavaScript 和 cookie,我不会过度担心使代码可降解)本网站仅供我们公司的客户使用,我们几乎规定了使用我们网站的条款和条件)。

现在,经过一些研究(GIYF 和所有这些),似乎最好的方法是让 JavaScript 设置 cookie,从而用一块石头杀死两只鸟。诚然,我们将无法将失败缩小到“一个或另一个”,但我试图让这个(对我来说)尽可能简单。即便如此,我不确定我是否以正确的方式去做这件事。这是我的 .js 文件中的代码:

function error_reporting (level) {
 // http://kevin.vanzonneveld.net
 // +   original by: Brett Zamir (http://brett-zamir.me)
 return this.ini_set('error_reporting', level);

}

var set = $(document).getUrlParam('set');

if(set !== 'yes') {

  var date = new Date();
  var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);

  $.cookie('candcptest', 'ok', {

    expire: midnight,
    path: '/',
    domain: 'candcp.rossdb.net',
    secure: false

  }, function() {

    window.document.reload();

  });

} else {

  var candcptest = $.cookie('candcptest');

  if(candcptest !== undefined) {
    var ccptresult = 'Thank you.  Your browser is configured to allow JavaScript and accept cookies.  You may proceed with login';
  } else {
    var ccptresult = 'Thank you.  Unfortunately, your browser is not configured correctly.  Please check your settings or contact the Dev Team for assistance';
  }
};

(我之前写了一个非常基本的测试来允许在 PHP 中使用 cookie,然后修改它来创建它,所以上面的内容可能与它本身的标记相去甚远)

现在,理想情况下,我想使用 jQuery,因为这似乎是一项非常适合它的任务,但显然,我不知道如何去做。这是我的 HTML 文件中的内容:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="styles/global.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.js"></script>
    <script type="text/javascript" src="js/jquery.getUrlParam.js"></script>
    <script type="text/javascript">
      $(function() {
        $('a#ccpt').click('js/candcptest',function() {
          $('div#ccptresult').append(result);
        });
      });
    </script>
  </head>

  <body class="body">
    <table width="1000" border="0" cellpadding="0" cellspacing="0" align="center" bgcolor="#FFFFFF">
      <tr>
        <td width="1000" height="545" align="center" valign="middle">
          <table border="0" align="center">
            <tr>
              <td width="750" height="150" align="center" class="text0down">
                This website makes use of JavaScript and cookies and it is essential that your browser is configured correctly in order for this website to work. If you are unsure if your browser is set up correctly, click <a href="#" class="text0down" id="ccpt">here</a> to test.
                <div id="ccptresult"></div>
              </td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </body>
</html>

现在,这个想法(以防我的 js 太难以理解以至于你无法弄清楚我要做什么)是,当用户单击链接(a#ccpt)时,js 将执行并且结果( var result) 将显示在 div (div#ccptresult) 中。

我认为不言而喻,测试页面绝对没有任何作用,所以有些事情严重不正确,但考虑到直到 7 天前,我从未编写过一行 js 代码,所以我的知识还差得很远也就不足为奇了足以弄清楚我自己哪里出错了。

任何帮助将不胜感激。

4

1 回答 1

0

if i have understood correctly

$(function() {
        $('a#ccpt').click(function() {
          //execute the js function here
          $('div#ccptresult').append(result);
        });
      });
于 2013-03-06T13:32:58.580 回答