How to add a Jquery library to Magento 1.9

Asked

Viewed 297 times

0

I was needing to add a Jquery library in Gento 1.9, to use on one of my phtml pages, to insert masks in my fields, such as Cpf, cnpj and etc ...

I have done a lot of research and have observed that the subject is very scarce, but very necessary, and I want to share that knowledge with you. There are several details to consider, but with a lot of patience it works, I discovered the correct and indicated method to do this operation, and I also discovered the quick method, in case the first method does not work.

1 answer

0


Well I researched a lot, and got two solutions, the first one is like this, you add the Jquery.js file you want in the directory Magento/js/jquery/name-of-your-lib.js.

Once this is done, you will search for the page.xml file, and add an addJs method that points to the location where you entered your.js file. (addJS function, I will explain below)

Location ex: app/design/frontend/default/seutema/layout/page.xml.

Right, coming to page.xml you will see a method calling addJS which is basically a native Brazilian function that aims to search in the Brazilian JS directory, the file you want to add. Pay close attention here, it is already set to search in the folder JS So basically, you will just type after this folder.

So let’s for example add a Jquery.js library:

<action method="addJs"><script>jquery/nome-da-sua-lib-jquery.js</script></action>

Note that I did not type js/jquery/..... I just typed in jquery/.... thus following what I said above, that js is already set and does not need to be referenced in your information about the directory.

Following these steps you will already have in your Magento your Jquery library, and then just use for your will. To know that it’s working, reload the user, activate the browser console and search the html for the head tag and see the jquerys built in there, if it’s appearing there, it’s ok.

IF YOU DID NOT SUCCEED USING THE FIRST METHOD(MOST INDICATED) ... Remember it may be easier, but I advise you to use the first way...

SECOND FORM

It’s basically the first thing you think about doing something like.

<script type="text/javascript" src="<?php echo $this->getJsUrl('jquery/nome-da-sua-lib-jquery.js')?>"></script>

Concluding remarks: Jquery Magento syntax 1.9

In your.phtml file, when you try to use a function in your Jquery, using your library you just added, and receive some error. Try using this syntax style:

jQuery('input[id="billing:exemplo"]').mask("(00)00000-0000");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.