Skip to content

Instantly share code, notes, and snippets.

@vinaysikarwar
Last active August 29, 2015 14:05
Show Gist options
  • Save vinaysikarwar/2d36630d843d1399153d to your computer and use it in GitHub Desktop.
Save vinaysikarwar/2d36630d843d1399153d to your computer and use it in GitHub Desktop.
Avoid jQuery Conflict in merging js
In magento if you merge the js file from enabling the merge js files setting in admin, and if you face the jQuery conflict issue, then you can find the solution in this gist.
if you go to the Mage_Page_Block_Html_Head class file and see the function getcssjshtml then you will know what this function is doing and what i have done to solve the js conflict issue.
One imp[ortant thing you have to take care in this that your jQuery files should be in skin folder and javascript prototype js file files should be in root js folder.
public function getCssJsHtml()
{
// separate items by types
$lines = array();
foreach ($this->_data['items'] as $item) {
if (!is_null($item['cond']) && !$this->getData($item['cond']) || !isset($item['name'])) {
continue;
}
$if = !empty($item['if']) ? $item['if'] : '';
$params = !empty($item['params']) ? $item['params'] : '';
switch ($item['type']) {
case 'js': // js/*.js
case 'skin_js': // skin/*/*.js
case 'js_css': // js/*.css
case 'skin_css': // skin/*/*.css
$lines[$if][$item['type']][$params][$item['name']] = $item['name'];
break;
default:
$this->_separateOtherHtmlHeadElements($lines, $if, $item['type'], $params, $item['name'], $item);
break;
}
}
// prepare HTML
$shouldMergeJs = Mage::getStoreConfigFlag('dev/js/merge_files');
$shouldMergeCss = Mage::getStoreConfigFlag('dev/css/merge_css_files');
$html = '';
foreach ($lines as $if => $items) {
if (empty($items)) {
continue;
}
if (!empty($if)) {
// open !IE conditional using raw value
if (strpos($if, "><!-->") !== false) {
$html .= $if . "\n";
} else {
$html .= '<!--[if '.$if.']>' . "\n";
}
}
// static and skin css
$html .= $this->_prepareStaticAndSkinElements('<link rel="stylesheet" type="text/css" href="%s"%s />'."\n",
empty($items['js_css']) ? array() : $items['js_css'],
empty($items['skin_css']) ? array() : $items['skin_css'],
$shouldMergeCss ? array(Mage::getDesign(), 'getMergedCssUrl') : null
);
// static and skin javascripts
$html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
empty($items['js']) ? array() : $items['js'],
empty($items['skin_js']) ? array() : array(),
$shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
);
// static and skin javascripts
$html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
empty($items['js']) ? array() : array(),
empty($items['skin_js']) ? array() : $items['skin_js'],
$shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
);
// other stuff
if (!empty($items['other'])) {
$html .= $this->_prepareOtherHtmlHeadElements($items['other']) . "\n";
}
if (!empty($if)) {
// close !IE conditional comments correctly
if (strpos($if, "><!-->") !== false) {
$html .= '<!--<![endif]-->' . "\n";
} else {
$html .= '<![endif]-->' . "\n";
}
}
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment