So lately I've been running a lot of jQuery on my Wordpress sites. Ive been getting the error of "Invalid or unexpected token" a lot, and found what causes it, just maybe looking for a way to fix it.
So basically this doesnt work:
jQuery(".someClass").prepend("
<h2>Some text</h2> ");
But this does:
jQuery(".someClass").prepend("<h2>Some text</h2>");
What makes it a bit hard for me is that my code is sometimes being echo'ed by php, so the above code is a result of this:
$descriptionHeadJQuery = '<script>
jQuery(".portfolio_images").prepend("' . $descriptionHead . '");</script>';
I'm sure I can get all the spacing right and the line breaks to get it to work properly, but I'm also sure that there is someone out here with a better solution for me :)
Thank you
VoidZA
In your PHP, removes all line breaks before you echo:
$lookNoLineBreaksMom = preg_replace( "/\r|\n/", "", $yourString );
as a nifty little function, too:
function look_mom_no_line_breaks($brokenString) {
echo preg_replace( "/\r|\n/", "", $yourString );
// or just return it:
// return preg_replace( "/\r|\n/", "", $yourString );
}
Related
It would seem this is not a new question, but after reading all the answer I can find, I'm still stuck.
I have a bit of php into which a snippet of javascript has been inserted:
<?php
function post_iframe_message( $msg ) {
?>
<script>
window.onload = function msgchild() {
console.log("called msgchild");
// obj = JSON.parse(JSON.stringify("Hello from parent"));
var val = "<?php echo $msg ?>";
obj = JSON.parse(var);
var e = document.getElementById('iframepm');
console.log(e);
e.contentWindow.postMessage(obj,'*');
}
</script>
<?php
echo "<script>iframepm.contentWindow.postMessage($msg,'*');</script>";
}
?>
I am calling this function with:
$current_user = wp_get_current_user()
post_iframe_message(json_encode($current_user));
I don't think the Wordpress bit is relevant other than to provide context for the variable object $current_user.
When this loads, I receive an 'Uncaught SyntaxError: Unexpected identifier' error in the DevTools (Chrome) console.
If I comment out the (attempted) parsing of the $msg object and simply pass the string literal "Hello from parent" everything works great i.e. postMessage succeeds without error.
This is so simple that it kills me to ask the question here, but at this point I'm just tucking my pride up in a little ball and putting it in the corner. What obvious bit am I missing?
My line in question is line 29 of my code. The syntax error is being reported on line 346 which hints at something really stupid like a missed quote. Again, swapping comments to:
obj = JSON.parse(JSON.stringify("Hello from parent"));
// var val = "<?php echo $msg ?>";
// obj = JSON.parse(var);
and everything works. What am I missing?
Thanks in advance and I hope this has provided you with a good newbie chuckle. :-)
I have used below script sometimes it's work but sometimes it's gives error in console that is Uncaught SyntaxError: Unexpected token < on first line.
$(<?php echo '"#'.$itemid.'"'; ?>).click(function() {
$("#options").modal(options).modal('openModal');
});
Even in inspect element in html the result is showing
$("#2735").click(function() {
$("#options").modal(options).modal('openModal');
});
Not getting what is the issue here..i used this script Script is here
i don't know what's happen but in a simple way you can write
$("#<?php echo $itemid; ?>")
is what itemid is always an integer ?
maybe is more secure
$("#<?php echo intval($itemid); ?>")
maybe in some case $itemid is null or empty and the client code will be
$("#")
or maybe an invisible char like \n ?
but i don't think your problem is located here
"Uncaught SyntaxError: Unexpected token < " in first line
I've a PHP webservice and when I call it with $.getJSON it goes to the .fail callback.
The problem appears only when i do an include_once at PHP. If I copy/paste what the included file does to the main php it works fine.
The responsetext is what I expected (a JSON with the data asked) and the status is 200, but inside the .fail callback.
It's strange. Anyone knows if there's any problems using include_once?
Then piece of PHP code is:
$bCarregat = include_once("sql/". $sConsulta .".php");
and if i replace it with:
$sSqlBase = "SELECT usuari, nom, moduls";
$sSqlBase .= " FROM www_usuari";
$sSqlBase .= " WHERE 1=1";
if (isset($aRestr['USUARI']) AND $aRestr['USUARI'] != "") {
$sSqlBase .= " AND usuari = '". str_replace("'", "''", $aRestr['USUARI']) ."'";
}
it works.
I solved the problem saving the secondary php file (the included) in UTF-8 without BOM.
I didn't know about the existence of different UTF-8 formats, but a little beginning char at response text (\ufeff) made me suspect.
I apreciate all your frustrated attempts to help me ;-P they have not been i vain.
I was able to close the window. But with the timeout, it does not seem to work.
This test.php was called by the submit button's action on another window. If I remarked out all window closing script lines, then this "Sending ... This window will close itself after sending." will show up.
This echo "<script>window.close();</script>"; will close this window without showing any echo. The other 3 lines, all I see is a blank window and not being closed at all. Only on the Chrome I got a Server 500 error. I tried on Firefox, Safari, and Chrome.
Any suggestions?
test.php contains:
<?php
echo "Sending ... This window will close itself after sending.";
echo "<script>window.close();</script>"; // this line works
// echo "<script>setTimeout("window.close()", 5000);</script>";
// <script type="text/javascript">setTimeout("window.close();", 3000);</script>
// echo "<script type="text/javascript">setTimeout( function() { window.close(); }, 3000);</script>"
?>
<script type="text/javascript">setTimeout("window.close();", 2000);</script>
Tell me if this works, of course change 2000 to what you want ;)
You should pass a function to setTimeout().
Try this:
<script>setTimeout(function(){ window.close();}, 5000);</script>
UPDATE on your update:
Error 500 means "Internal server error", there is something wrong with your script, not the resulting page.
I assume that you are not posting the real script, but there is something else wrong in it besides what you output.
Did you really wrote double quotes inside PHP string?
Because what you are really doing is writing a compile error, something like that is illegal PHP code:
echo "something "quoted" something";
I still suggest you to write a proper function and not a string, but if you must, at least escape the double quote, or use the single quote to start PHP constant string if you don't have to parse variables in it.
echo 'something "quoted" something';//I prefer this
or
echo "something \"quoted\" something";
//but this still works, altough parser will try to find variable names
I'm using CodeIgniter form helper, and this is what it does:
Uncaught SyntaxError: Unexpected token ILLEGAL
The code:
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"') ?>');
As you can see, I'm using a PHP inside of a JS, when I do <?= 'Test' ?> it works.
So it seems like its related with the CodeIgniter function.
As far as i know this error message can be caused by unknown/wrong characters in the code, and from what I saw in the firebug, this CI function is generating text with tabs and line breaks... and that is my problem I guess.
I may be wrong, so please correct me if so.
I will appreciate any solution for this problem.
Chances are you're screwing up your quotes and you need to change the way you're passing that last parameter to the dropdown.
$class = 'class="span3"';
jQuery('#window-1').append('<?= form_dropdown("hfdata", $hf_arr, set_value("hfdata"), $class) ?>');
To explain how PHP and Javascript works: Php is executed on the server, this give html output. You browser will get that output and can do anything with it. From this point javascript can do his job.
You are echoing the php function call to the users browser. Javascript can't call the php function. If you want to do this, then you need to make a php file call that returnes the result you want. But I guess there is an other problem. ( if you want to do this, escape the ' characters)
You need to execute the PHP code on the server. This will give a result. You send this result to the client. On the clients PC javascript will execute your javascript code.
If you want to generate with PHP the javascript code, then you can do something like this ( in PHP on the server!):
jQuery('#window-1').append('<?= form_dropdown('hfdata', $hf_arr, set_value('hfdata')); ?> ');
If this isn't what you want, then you are working on a design problem. Then it's a kind of dead end.
I found the answer myself... As I said, it was caused by the HTML output that was returned from the from_dropdown.
The solution is simple, remove all unwanted characters like line breaks:
<?php
$prepare = preg_replace('/^\s+|\n|\r|\s+$/m', '', form_dropdown('hfdata', $hf_arr, set_value('hfdata'), 'class="span3"'));
?>
jQuery('#window-1').append('<?= $prepare ?>');