php - Joomla/Php/Jquery/Css 中的 Switchstyle 问题

标签 php jquery css joomla styleswitching

我一直在开发一个网站(现在这里作为示例:http://neurotoxine.mine.nu/gloom),我从样式切换器代码中得到了一个奇怪的行为。它很大,所以请耐心等待。

首先,你可以先去我指定的站点看看。 你得到的是一个 joomla 页面,使用 Jquery

var $j = jQuery.noConflict();

为了避免mootools冲突。 然后,我使用了这些:

<?  // Checks for, and assigns cookie to local variable:
if(isset($_COOKIE['style']))
{   $style = $_COOKIE['style'];
}
// If no cookie is present then set style as "red" (default):
else {    $style = 'red';
}
?>

这只是为了cookie设置,如果没有设置cookie那么$style=red,这个变量会附加到CSS中。像这样:

<link rel="stylesheet" type="text/css" href="<?php echo $this->baseurl ?>/templates/wh1/css/template_<?php echo $style ?>.css" media="screen" />

第一个代码,$this->baseurl=directory 用于 joomla 安装,在本例中为 gloom。 第二个,echo $style,将写入 cookie 加载值或从选项分配的值,如果您是第一次到达那里,那么您将获得 RED 作为值,template_red.css 将是整个网站。

有一个 JS,可以执行一些 jquery 技巧:

 jQuery.fn.styleSwitcher = function(){
  $j(this).click(function(){
      // We're passing this element object through to the loadStyleSheet function.
      loadStyleSheet(this);
      // And then we're returning false.
      return false;
  });
  function loadStyleSheet(obj) {
    $j('#preloader')
    // Now fade in the div (#preloader):
    .fadeIn(500,function(){
      // The following will happen when the div has finished fading in:
      // Request PHP script (obj.href) with appended "js" query string item:
      $j.get( obj.href+'&js',function(data){
        // Select link element in HEAD of document (#stylesheet) and change href attribute:
        $j('#stylesheet').attr('href','css/' + data + '.css');
        // Check if new CSS StyleSheet has loaded:
        cssDummy.check(function(){
          // When StyleSheet has loaded, fade out and remove the #overlay div:
          $j('#preloader').fadeOut(500);
        });
      });
    });
  }
  // CSS DUMMY SECTION
  var cssDummy = {
    init: function(){
      // Appends "dummy-element" div to body:
      $j('<div id="dummy-element" style="display:none" />').appendTo('body');
    },
    check: function(callback) {
      // Checks if computed width equals that which is defined in the StyleSheets (2px):
      if ($j('#dummy-element').width()==2) callback();
      // If it has not loaded yet then simple re-initiate this function every 200 milliseconds until it had loaded:
      else setTimeout(function(){cssDummy.check(callback)}, 200);
    }
  }
  cssDummy.init(); }

然后,我在我的页面中启动该功能:

$j(document).ready(function(){
$j('#style-switcher a').styleSwitcher();
});

我通过这样的链接调用它:

<a href="<?php echo $this->baseurl ?>/templates/wh1/style-switcher.php?style=fire">img</a>

最后,stylesswitcher.php代码在这里:

<?php
$style = $_GET['style'];
setcookie("style", $style, time()+604800*24); // 604800 = amount of seconds in one week *4=1 month *24=1/2 year *48=1 year
if(isset($_GET['js']))
echo $style;
else header("Location: ".$_SERVER["HTTP_REFERER"]); ?>

现在,问题是,当我在一个网站上测试它时,一切正常(http://neurotoxine.mine.nu/wht/index-test.php - 样式转换器的链接位于顶部,它们的左侧显示主题)。但是当我在 joomla 模板中插入整个代码时,整个系统都失败了。我在这里和那里做了一些修复(主要是 $j 声明),然后我意识到这可能与模板的路径有关,所以我测试了许多类型的声明、绝对路径、相对路径等,但没有任何反应。

然后我将 stylesswitcher.php 复制到 joomla 的根目录 (neurotoxine.mine.nu/gloom) 并检查 ti 是否可以在那里工作,然后我得到一个空白页面。我单击返回,然后哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇蛇说明,但我不知道要在那里更改什么才能使其正常工作。

Joomla 在其模板的标题中给出声明:

<base href="http://neurotoxine.mine.nu/gloom/" />

我不知道这是否对 http_referer 的工作方式有影响,但我禁用了它,网站仍然做同样的事情,点击一个样式,页面空白,点击返回,瞧,样式已经改变了但未能检索到我所在的页面。

一些想法?任何帮助都可能有用。

最佳答案

Referer 仅在您单击链接时设置(而不是在您直接在地址栏中输入时)。事实上,浏览器会在用户单击此链接之前告知站点用户去过的位置,因此您不能 100% 信任它(请参阅 PHP Manual)。我会将上次访问的页面存储在 $_SESSION 中并重定向到该页面;如果它是空的,则重定向到 $this->baseurl 。 '/index.php'。

只有相对链接才需要base-Tag,例如

<a href="index.php">

甚至:

<a>

另一个提示:在从 cookie 获取样式之前,检查它是否存在(硬编码所有可用模板或 file_exists())。如果不是,则采用默认值(红色)。

关于php - Joomla/Php/Jquery/Css 中的 Switchstyle 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1320501/

相关文章:

php - DIV 重叠图像而不是在 Chrome 中位于下方,在其他浏览器中看起来正确

php - 结帐字段 - 使 billing_address_2 高于 billing_address_1

javascript - 选中一个复选框以选中所有,自定义复选框

javascript - Jquery 用新值替换链接

php - CakePHP 从 URL 中删除索引操作

PHP - 复制或不复制,这是变量?

javascript - 添加新行后在 jQuery 中搜索表

javascript - Bootstrap 响应表单击以展开 div 不起作用

html - css 文件上的颜色不起作用

javascript - 如何在用户键入时更改输入的类?