Magento escape string for javascript

Is there a helper function that will be correctly removed from the string, which will be displayed as a single quotation mark, the Java letter quoted by the string?

I know jsQuoteEscape, but it only processes quotes and does not process \ n and \ r, etc.

so if my line is 'line1 \ nlineb' (i.e. two lines with a new line between them)

and i use

var jsvar='<?php echo $this->helper('myextension')->jsQuoteEscape($mystring); ?>'; 

I will get into the displayed content

  var jsvar='line1 line2'; 

which is a syntax error.

Thank you Eyal

+6
json javascript php escaping magento
source share
1 answer

Yes

 $string = 'Hello There'; var_dump( Mage::helper('core')->jsonEncode($string) ); var_dump( json_encode($string) ); 

I was never clear if this encoding of string data types other than objects like a javascript string is a side effect of JSON encoding or if it is true according to Hoyle Crockford JSON, so I always love to wrap my strings in an object when I pass them around

 $o = new stdClass(); $o->param = 'This is my Param'; $json = json_encode($o); echo 'var data = ' . $json . ';' . "\n"; echo 'var jsdata = data.param'; 

Here's how you handle this with javascript. There is no method that was specifically created for this. If you are interested in finding out what auxiliary methods you have in a block, check out the methods in

 app/code/core/Mage/Core/Block/Abstract.php app/code/core/Mage/Core/Block/Template.php 

and if you are dealing with a template that part of the block is above the chain, get its class, and then check its definition

 var_dump get_class($this); 
+5
source share

All Articles