2011-01-20 72 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()通話。

相關問題