Execute SMT code in Javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kwgarrett
    Mivite

    • Oct 2008
    • 135

    #1

    Execute SMT code in Javascript?

    I have a Google Maps javascript and I'd like to find a way to let me know when an address fails geocode lookup. I have tried document.write() methods to try to pass the result in a hidden Additional Checkout Pro field. So far, no luck. Interestingly, this code functions (sort of):

    Code:
    <script type="text/javascript">
    
    
        function loadthemap() {
    
    
        document.write(
        
        "<mvt:item name='ry_toolbelt' param='assign|g.myvar|g.add3' />"
        
        );
        }
    </script>
    Where "g.add3" is an ACD global (no significance -- it was just handy). It actually does put the value of g.add3 into g.myvar. Not that that does me any good -- its just the only thing I could get to work. I want to put a string as the argument, but I have not found a way to escape the string delimiters in a way that Toolbelt will accept.

    I would assume that for security reasons, what I want to do would be impossible. Yet, I have had this limited success. Anyone out there done this sort of thing with success?
  • kwgarrett
    Mivite

    • Oct 2008
    • 135

    #2
    Re: Execute SMT code in Javascript?

    Further development. I do not understand how this can work. But it does. And there's an extra opening "(" that seems to be necessary to make it work.

    Code:
    <script type="text/javascript">
    
    
    	function loadthemap() {
    
    
    	document.write((escape(<mvt:item name='ry_toolbelt' param="assign|l.all_settings:mysupervar|'yessireebob'" />));
    	
    	}
    
    
    </script>

    Result:

    l.all_settings:mysupervar &mvt:mysupervar; yessireebob

    Go figure. After my original post I thought "Of course it can't work. Javascript is interpreted by the browser." But it does work.

    Comment

    • Kent Multer
      Mega Mivite









      • Mar 2006
      • 3251

      #3
      Re: Execute SMT code in Javascript?

      Probably the Toolbelt item is being executed by the store when it renders the page. That would explain why the variable assignment is done. But what do you see when you look at the HTML source?

      There's probably a way to do what you're trying, but I don't quite follow your description. If you want to trigger some kind of store behavior when an error occurs in JavaScript, you'll have to write some custom JS that can trigger the reloading of the page, or maybe the loading of a new page with a map-error message. If you just want to display an error message to the customer, you can use a Javascript alert(), or something fancier such as a pop-up div.

      HTH --
      Kent Multer
      Magic Metal Productions
      http://TheMagicM.com
      * Web developer/designer
      * E-commerce and Miva
      * Author, The Official Miva Web Scripting Book -- available on-line:
      http://www.amazon.com/exec/obidos/IS...icmetalproducA

      Comment

      • kwgarrett
        Mivite

        • Oct 2008
        • 135

        #4
        Re: Execute SMT code in Javascript?

        No, the idea is to alert ME as to what addresses cause the map to fail. I already discovered that I have to escape single quotes from the address. And Google Maps geocoder doesn't understand Puerto Rico as a state -- only as a country. Are there more gotcha's that I don't know about? That's why I want to know the addresses that fail.

        Looking at the HTML RENDERED source shows only

        <script type="text/javascript">function loadthemap() {document.write((escape());}</script>

        Comment

        • Kent Multer
          Mega Mivite









          • Mar 2006
          • 3251

          #5
          Re: Execute SMT code in Javascript?

          Originally posted by kwgarrett View Post
          Looking at the HTML RENDERED source shows only
          <script type="text/javascript">function loadthemap() {document.write((escape());}</script>
          Yes, that seems to confirm my hunch about when the Toolbelt item is being processed.

          If you want the rendered page to notify someone other than the customer, you're going to need JS code that can reload the page, or load a new page, or use AJAX to send some sort of message to the store.
          Last edited by Kent Multer; 02-27-12, 09:34 AM.
          Kent Multer
          Magic Metal Productions
          http://TheMagicM.com
          * Web developer/designer
          * E-commerce and Miva
          * Author, The Official Miva Web Scripting Book -- available on-line:
          http://www.amazon.com/exec/obidos/IS...icmetalproducA

          Comment

          • Bruce - PhosphorMedia
            Developer Partner











            • Mar 2006
            • 11256

            #6
            Re: Execute SMT code in Javascript?

            Why not just pass the addr to an outside script where it can send the values to google maps and then, if not a match, send you an email. could be done in almost any server based script. Toolbelt should be able to pass the need variables (although I think they are available on the subsequent page load). You'd probably have to look for another API method other than javascript to actually send the request...but the whole thing would be a lot easier outside of merchant.
            Bruce Golub
            Phosphor Media - "Your Success is our Business"

            Improve Your Customer Service | Get MORE Customers | Edit CSS/Javascript/HTML Easily | Make Your Site Faster | Get Indexed by Google | Free Modules | Follow Us on Facebook
            phosphormedia.com

            Comment

            • RayYates
              Developer Partner



              • Dec 2006
              • 1517

              #7
              Re: Execute SMT code in Javascript?

              The assign command does not output anything. You can use the eval command or use storemorph to output the value.
              To pass store values to JavaScript, one of these examples should help .


              var my_js_variable = "&mvt:global:myvar;" ;
              Or
              var my_js_variable = "&mvt:mysupervar;" ;


              Then you can use my_js_variable in your JavaScript code.
              escape(my_js_variable)

              Alternatively this syntax should also work, but I normally use the method above.
              escape("&mvt:global:myvar;") OR escape("&mvt:mysupervar;")


              One more example:
              Using the Eval command you can manipulate the value and output it at the same time.

              <mvt:item name="ry_toolbelt" param="assign|g.zipcode|'5213'" />
              var zipcode = "<mvt:item name="ry_toolbelt" param="eval| padl( trim( g.zipcode ), 5, '0' ) " />";

              Result:
              var zipcode = "05213";
              Last edited by RayYates; 02-28-12, 06:37 AM.
              Ray Yates
              "If I have seen further, it is by standing on the shoulders of giants."
              --- Sir Isaac Newton

              Comment

              • kwgarrett
                Mivite

                • Oct 2008
                • 135

                #8
                Re: Execute SMT code in Javascript?

                Thanks, Ray. I might not be understanding your code correctly, but I am not trying to get a miva variable to use in JS, but to use a variable in JS to set a miva variable.

                The method I came up with achieved that but it's a hack and causes other problems. So I reassessed what I was trying to do and changed tactics.

                Thanks again,

                Kevin

                Comment

                • RayYates
                  Developer Partner



                  • Dec 2006
                  • 1517

                  #9
                  Re: Execute SMT code in Javascript?

                  Just to be clear, sorry if I'm stating the obvious, but I'm not exactly sure what you are trying to do.

                  The code in your Store page template is executed at the server, this includes Toolbelt.
                  The code in JavaScript is executed at the browser.

                  They execute independently and do not share variables unless you create some way for them to communicate.

                  What I showed was how to get a variable from your page template into your JavaScript so that the browser can do something with it.


                  It seems your function loadthemap() is calling the Google Map API. I recommend you call the API from the server with the Call command. Then if successful, you can do some thing with it, like outputting the JavaScript that creates the map, or take other action if it fails.

                  If you are trying to do the opposite, that is getting the server to know something that happened at the browser, Your javascript will have to send it to the server, and the server will have to be coded to receive the information. Normally this can be done by

                  A) populating a hidden form then submitting it (this can be done with an iframe) or
                  B) creating a URL that contains the variables you want to send and sending that via Javascript or
                  C) using ajax to send the data.

                  Not to hijack this thread with self promotion, but that's what I do with the My Map Locations module.
                  Ray Yates
                  "If I have seen further, it is by standing on the shoulders of giants."
                  --- Sir Isaac Newton

                  Comment

                  • kwgarrett
                    Mivite

                    • Oct 2008
                    • 135

                    #10
                    Re: Execute SMT code in Javascript?

                    I'll look into that module. I wish I had known about it before. Might have saved me lots of time. But for now, I'm up and running with my map application and onto more store improvements. What I was trying to do was let the store know something that my Javascript knows. In this case, some data for Additional Checkout Data Pro so that when we download the orders into Shipworks, I can easily see when an address failed geocode lookup.

                    Here's a sample page with the (so far) competed map application.

                    http://www.podmerchant.com/google-map-sample.html

                    Kevin Garrett

                    Comment

                    Working...
                    X