2011-06-01 48 views
2

我將此添加到我的WordPress page&&是打破了網頁在WordPress

if (script.readyState && script.onload!==null){ 
    script.onreadystatechange= function() { 
     if (this.readyState == 'complete') mce_preload_check(); 
    } 
} 

&&正在轉向

if (script.readyState && script.onload!==null){ 

我在WordPress的HTML視圖中粘貼此,我確信這很好,但WordPress不斷顯示這一點。如何解決這個問題?

+0

你把這個JS代碼在一個職位?或者你在編輯主題的header.php文件? – 2011-06-01 02:36:43

+0

把js放在帖子/頁面 – Trace 2011-06-01 02:43:42

+0

你想讓這個在帖子中顯示給用戶,還是這個用於你自己的網站執行? – 2011-06-01 02:50:55

回答

7

您需要禁用WP的自動配置。即使在HTML編輯器中,WP也會自動格式化,而空格和換行符會打破你的javascript。

使用這個插件http://wordpress.org/extend/plugins/wp-no-format/

更新2015年4月8日:插件是過時,但仍然對我的作品。

這也適用:直接添加插件添加到functions.php和支架將您的JavaScript在<!-- noformat on --><!-- noformat off -->標籤

添加到functions.php文件:

function newautop($text) 
{ 
    $newtext = ""; 
    $pos = 0; 

    $tags = array('<!-- noformat on -->', '<!-- noformat off -->'); 
    $status = 0; 

    while (!(($newpos = strpos($text, $tags[$status], $pos)) === FALSE)) 
    { 
     $sub = substr($text, $pos, $newpos-$pos); 

     if ($status) 
      $newtext .= $sub; 
     else 
      $newtext .= convert_chars(wptexturize(wpautop($sub)));  //Apply both functions (faster) 

     $pos = $newpos+strlen($tags[$status]); 

     $status = $status?0:1; 
    } 

    $sub = substr($text, $pos, strlen($text)-$pos); 

    if ($status) 
     $newtext .= $sub; 
    else 
     $newtext .= convert_chars(wptexturize(wpautop($sub)));  //Apply both functions (faster) 

    //To remove the tags 
    $newtext = str_replace($tags[0], "", $newtext); 
    $newtext = str_replace($tags[1], "", $newtext); 

    return $newtext; 
} 

function newtexturize($text) 
{ 
    return $text; 
} 

function new_convert_chars($text) 
{ 
    return $text; 
} 

remove_filter('the_content', 'wpautop'); 
add_filter('the_content', 'newautop'); 

remove_filter('the_content', 'wptexturize'); 
add_filter('the_content', 'newtexturize'); 

remove_filter('the_content', 'convert_chars'); 
add_filter('the_content', 'new_convert_chars'); 
+0

已過時,不再有效。 – TARKUS 2014-11-01 19:59:36

+0

4/08/2015:插件仍然有效;看到更新的答案。 – markratledge 2015-04-08 15:23:54

0

另一種選擇是做一個shortcode。在這個例子中,短碼僅在包含xy的屬性時纔會打印,例如:[myscript x="10" y="20"]。我正在使用一個簡單的腳本,顯示帶有屬性值的JS警報對話框。

add_shortcode('myscript', 'sample_shortcode_so_6195635'); 

function sample_shortcode_so_6195635($atts, $content = null) 
{ 
    if(isset($atts['x']) && isset($atts['y'])) 
    { 
     $x = $atts['x']; 
     $y = $atts['y']; 

     // See: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc 
     $html = <<<HTML 
     <button onclick="myalert()">Show Shortcode Atts</button> 

     <script type="text/javascript"> 
     function myalert() 
     { 
      if($x < 10 && $y < 20) 
       alert('X less than 10 and Y less than 20'); 
      else 
       alert('other'); 
     } 
     </script> 
HTML; 
     return $html; 
    } 
}