Link to home
Start Free TrialLog in
Avatar of Solenthaler
Solenthaler

asked on

PHP call to javascript localStorage.setItem special Chars fails

The code below is having a problem with the special characters. Does anybody know why?

<script type="text/javascript">
function enter_user_email(str){
   localStorage.setItem('user_email', str);
}
</script>

<?php
$usr_email= "aaaaaa@bbbbb.xx";
echo "<script type=\"text/javascript\">enter_user_email(\"$usr_email\");</script>";
?>
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What special characters?  Is there some confusion about UTF-8 encoding, maybe?
Avatar of Solenthaler
Solenthaler

ASKER

If $usr_email= "aaaaaa@bbbbbxx"; or  "aaaaaabbbbb.xx"; the call to the function succeeds.
However if $usr_email = "aaaaaa@bbbbb.xx"; :
 -> the PHP call to the javascript function is failing and it echoes the following three chars: "); (the 3 last chars from to call code) I think, because of the special chars in $usr_email - the call fails. If i delete the "." and/or the "@" the call works perfect.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Ray
incredible - thanks for this! I will build up a Demo.
Some environment in my example:
 -> the code is in a self made module for Joomla 2.5 and will use Joomlas user management. Will come back to it on sunday.
Cheers Roland
Avatar of CEHJ
The other thing is that one can't assume that localStorage is available. e.g. it's been known for it to fail on Safari when private browsing is enabled. Maybe test first:

function isLocalStorageSupported() {
    var result = true;
    try {
	localStorage.setItem("foo", "bar");
	result = (localStorage.getItem("foo") == "bar");
    } catch (error) {
	result = false;
    }
    return result;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@CEHJ: I think that's a good idea - to test for support.  Trouble is, I don't know how to "degrade gracefully" if localStorage is not there.  Maybe set a cookie?  Or create a nag-page?  Not sure of the best practices in that case.

@Slick812: The example script parses correctly, but you're right that the quote marks can be confusing.  That's why I favor the use of HEREDOC notation (as shown above).  The quotes and apostrophes in a HEREDOC block have no meaning to PHP so you can concentrate on getting the script right for JavaScript, only.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kickstarter.com uses a nag-page if you don't accept their cookie, and it seems to work in a way that makes sense.  But sometimes I feel like clients who don't return cookies or run javaScript are so far out on the edge that we can ignore them without any measurable business impact.  Given that you can't use FB, Twitter, etc., without that functionality it seems like the number of people who disable these things is pretty small.  But I've not seen a survey recently.
Hi experts
finally i made it (with your help - of course). Surprisingly a Joomla Plugin "e-mail cloake" caused the problem. The e-mail string has on the flight been replaced by the encrypted string.  {emailcloak=off} did the trick. Thanks a lot.