2011-01-20 76 views
0

我有一个PHP页面,它可以工作,它也有一些顶部的JavaScript,我在HEAD标记之间。我的PHP使用“header(”location:/“);”代码将用户重定向到随机点的index.php。我已经缩小了我的不能修改标题信息错误到这一片JavaScript,如果我把它拿出来它工作正常,当我把它放回失败。引起的JavaScript无法修改标题信息PHP中的错误

我知道错误是由html之前输出的东西引起的,但我不知道如何读取或理解JavaScript。有人能指出我可以或应该改变什么吗?

<script type="text/javascript"> 
    $(document).ready(function(){ 
    //jCarousel Plugin 
    $('#carousel').jcarousel({ 
     vertical: true, 
     scroll: 1, 
     auto: 2, 
     wrap: 'last', 
     initCallback: mycarousel_initCallback 
    }); 
    //Front page Carousel - Initial Setup 
    $('div#slideshow-carousel a img').css({'opacity': '0.5'}); 
    $('div#slideshow-carousel a img:first').css({'opacity': '1.0'}); 
    $('div#slideshow-carousel li a:first').append('<span class="arrow"></span>') 
    //Combine jCarousel with Image Display 
    $('div#slideshow-carousel li a').hover(
     function(){ 
      if (!$(this).has('span').length){ 
       $('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'}); 
       $(this).stop(true, true).children('img').css({'opacity': '1.0'}); 
      } 
     }, 
     function(){ 
      $('div#slideshow-carousel li a img').stop(true, true).css({'opacity': '0.5'}); 
      $('div#slideshow-carousel li a').each(function(){ 
       if ($(this).has('span').length) $(this).children('img').css({'opacity': '1.0'}); 
      }); 
     } 
    ).click(function(){ 
     $('span.arrow').remove();   
     $(this).append('<span class="arrow"></span>'); 
     $('div#slideshow-main li').removeClass('active');   
     $('div#slideshow-main li.' + $(this).attr('rel')).addClass('active'); 
     return false; 
    }); 
}); 
//Carousel Tweaking 
function mycarousel_initCallback(carousel){ 
    // Pause autoscrolling if the user moves with the cursor over the clip. 
    carousel.clip.hover(function(){ 
     carousel.stopAuto(); 
    }, function(){ 
     carousel.startAuto(); 
    }); 
} 
</script> 
+0

我没有看到这个JavaScript里面的任何PHP代码。这是你的php文件中的完全相同的代码,还是在做了php页面的“view-source”后复制了上面的代码? – 2011-01-20 07:28:16

回答

1

您不能在header('Location: /');之间的任何地方。它应该在任何输出之前发送。如果你想以后重定向用户,使用HTML重定向http://php.net/manual/en/function.header.php

读到这里的吧。

是这样的:<meta http-equiv="Refresh" content="5;url=http://www.abc.com" />

如果我不明白你的问题,这有没有关系,你已经把jQuery代码

+0

我的页面一直包含html和php。 header(location:/“);位于 if语句中,如果语句要么执行包含头函数的函数,要么在显示html的情况下关闭php?>显示html,然后打开<?php以添加end} – medoix 2011-01-20 05:49:44

+0

出现在``标签之外的每一个*字符*都会被发送到浏览器,并且在任何输出之后获得该PHP重定向代码,将会简单地破坏内容,如果您愿意的话,您也可以使用javascript进行重定向 – SuperSaiyan 2011-01-20 06:04:58

1

你不能调用头之前的任何HTML(”位置...

即使空间将打破它,你的PHP代码应该是在你的文件中的第一个字符,如果你想用头位置的命令如

--------- 
<?php 
    //your header location code 
?> 
--------- 

表示您的文件和您的PHP标记的虚线之间的任何内容都会破坏标题位置。

1

像其他人所说的,它不是jQuery代码本身。有<?php ?>标签以外的内容,在您拨打header()之前需要删除。如果你不能修改你的代码来解决这个问题,那么看看ar output buffering。这将允许您输出内容,然后仍然发出header()通话。

相关问题