我一直在开发一个网站(现在以这里为例:http : //neurotoxine.mine.nu/gloom),并且我从 styleswitcher 代码中得到了一个奇怪的行为。很大,请耐心等待。
首先,你可以先去我指定的地点看看。你有什么,它是一个 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 安装,在本例中是阴暗的。第二个,echo $style,将写入 cookie 加载值或从选项分配的值,如果你第一次到达那里,那么你会得到 RED 作为值,template_red.css 将是 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>
最后,styleswitcher.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 - stylechanger 的链接在它们的顶部和左侧说主题)。但是当我在 joomla 模板中插入整个代码时,整个系统都失败了。我在这里和那里做了一些修复(主要是 $j 声明),然后我意识到这可能与模板的路径有关,所以我测试了许多类型的声明、绝对路径、相对路径等,但没有任何反应。
然后我将 styleswitcher.php 复制到 joomla 的根目录 (neurotoxine.mine.nu/gloom) 并检查 ti 是否可以在那里工作,我得到一个空白页。我单击了返回,然后 WOW 样式转换器工作了,但是有些东西没有告诉返回到哪里,我认为它可能是标题(“位置:”.$_SERVER[“HTTP_REFERER”]); 指令,但我不知道要在那里改变什么才能使它工作。
Joomla 在其模板的标题中给出了一个声明:
<base href="http://neurotoxine.mine.nu/gloom/" />
我不知道这是否对 http_referer 的工作方式有影响,但我禁用了它,网站仍然这样做,单击样式,页面空白,点击返回,瞧,样式已更改但未能检索我所在的页面。
一些想法?任何帮助都可能有用。