0

I just need your help about my code. My problem is how can I access smarty variable within jquery or javascript file? Becuase my smarty variable is a URL request from my controller. And I need to use that variable for creating validation. Here's my code.

{$get.search_by} {**works without error**}

{literal}
    <script type="text/javascript">

        $(document).ready(function(){

            var dispatch = "{$get.search_by}"; //can't access

            var new_class = "it3 ir3 il3 jt10 jr05 jl05 kt03 kr04 kl04";
            var old_class = "it3 ib3 il3 jt05 jb05 jl10 kt04 kb04 kl03";

            var toggleState = true;

            //could not access
            if(dispatch == companies.catalog){
                alert("catalog");
            }else{
                alert("product search");
            }

            console.log(dispatch);
3
  • try this var dispatch = {$get.search_by}; Commented Nov 5, 2013 at 9:24
  • Ok i got an error. Unexpected token. My sample output for dispatch is products.search Commented Nov 5, 2013 at 9:28
  • 6
    try this var dispatch = '{/literal}{$get.search_by}{literal}'; Commented Nov 5, 2013 at 9:38

2 Answers 2

9

Try this code

var dispatch = '{/literal}{$get.search_by}{literal}'
Sign up to request clarification or add additional context in comments.

Comments

0

To make things cleaner, you can move the {literal} tag down and also escape the $get.search_by variable (in case search_by may have a string with a quote i.e. "let's try"):

<script type="text/javascript">
var dispatch = '{$get.search_by|escape:'javascript'}';

{literal}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.