Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3789504
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:10:01+00:00 2026-05-19T12:10:01+00:00

Is there a helper function that will properly escape a string to be rendered

  • 0

Is there a helper function that will properly escape a string to be rendered as a single quote quoted JavaScript string literal?

I know of jsQuoteEscape but it only handles quotes and does not treat \n & \r etc.

so if my string is ‘line1\nlineb’ (i.e. two lines with a newline between them)

and I use

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

I will get in the rendered content

    var jsvar='line1
line2';

which is a syntax error.

Thanks,
Eyal

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:10:02+00:00Added an answer on May 19, 2026 at 12:10 pm

    Yes

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

    I’ve never been clear if this encoding a non-object string datatypes as a javascript string is a side-effect of the JSON encoding, or if it’s true, according to Hoyle Crockford JSON, so I always like to wrap my strings in an object when passing 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';
    

    This is how you’d handle this with javascript. There’s no method that’s build specifically for this. If you’re interested in seeing the helper methods you do have available from a block, checkout the methods in

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

    and if you’re dealing with a template that’s part of a block higher up the chain, get its class and then check its definition

    var_dump get_class($this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Occasionally I will know that there is a .NET Framework function that returns a
Is there any built-in utility or helper to parse HttpContext.Current.User.Identity.Name , e.g. domain\user to
In the admin interface and newforms there is the brilliant helper of being able
In ASP.NET MVC is there an equivalent of the Html.ActionLink helper for Img tags?
Is there a better way to do this? I have an HTML helper extension
There is a conversion process that is needed when migrating Visual Studio 2005 web
There are two popular closure styles in javascript. The first I call anonymous constructor
Are there any html helpers for page navigation. eg. if i have 1000 records
Is there an easy way to have the form helpers set the tabindex parameter
There are two weird operators in C#: the true operator the false operator If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.