admin管理员组文章数量:1336380
I added this to my WordPress page
if (script.readyState && script.onload!==null){
script.onreadystatechange= function () {
if (this.readyState == 'plete') mce_preload_check();
}
}
and the &&
is being turned to
if (script.readyState && script.onload!==null){
I pasted this in WordPress HTML view and I made sure this was fine but WordPress keeps displaying this. How to address this?
I added this to my WordPress page
if (script.readyState && script.onload!==null){
script.onreadystatechange= function () {
if (this.readyState == 'plete') mce_preload_check();
}
}
and the &&
is being turned to
if (script.readyState && script.onload!==null){
I pasted this in WordPress HTML view and I made sure this was fine but WordPress keeps displaying this. How to address this?
Share Improve this question edited Aug 18, 2013 at 12:26 brasofilo 26.1k15 gold badges93 silver badges186 bronze badges asked Jun 1, 2011 at 2:32 Matt ElhotibyMatt Elhotiby 44.1k91 gold badges224 silver badges328 bronze badges 5- are you putting this JS code in a post? or are you editing the theme's header.php file? – Sujit Agarwal Commented Jun 1, 2011 at 2:36
- And you want this to be shown to the user in a post or is this for execution for your own site? – Sujit Agarwal Commented Jun 1, 2011 at 2:50
- I actually didnt add it...but i was responsible for fixing it – Matt Elhotiby Commented Jun 1, 2011 at 3:06
- i am responsible for fixing the errors, but i originally didnt put the js in the post....i was thinking of moving it out into an external js file though – Matt Elhotiby Commented Jun 1, 2011 at 3:18
- What about uploading the javascript file to another service and importing it to Wordpress from there? See stackoverflow./a/65674751/5802289 – J0ANMM Commented Jan 11, 2021 at 21:28
4 Answers
Reset to default 8You need to disable WP's autoformatting. WP will auto format even in the html editor, and the spaces and line breaks will break your javascript.
Use this plugin http://wordpress/extend/plugins/wp-no-format/
Update 4/08/2015: plugin is dated but still works for me.
This also works: add the plugin directly to functions.php and bracket the your javascript in <!-- noformat on -->
and <!-- noformat off -->
tags
Add to functions.php file:
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');
Another option is to make a shortcode. In this example, the shortcode will only be printed if it contains the attributes x
and y
, e.g.: [myscript x="10" y="20"]
. I'm using a simple script that shows a JS alert dialog with the attributes values.
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/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;
}
}
Make sure the following lines (either via inline PHP (you need a plugin for that) or in "functions.php") are being executed on your page:
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_content', 'wptexturize' );
remove_filter( 'the_content', 'convert_chars' );
Add this code snippet to your script
<script>
function logicalAND($opA, $opB) {
if($opA) {
if($opB) {
return true;
}
}
return false;
}
if (logicalAND(script.readyState, script.onload !== null)){
...
</script>
本文标签: javascriptampamp is breaking the page in WordPressStack Overflow
版权声明:本文标题:javascript - && is breaking the page in WordPress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742386357a2465114.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论