Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get Magento baseUrl through Javascript and then use it in jquery.hello-lightbox.min?

i'm trying to get Magento BaseUrl through javascript in head.phtml file, and then use it in jquery.hello-lightbox.min file, where i need the baseUrl to get some images.

Here's what i have in head.phtml file:

<?php $baseUrl = $this->getBaseUrl() ; ?>

<script type="text/javascript">

var baseUrl = <?php echo $baseUrl ; ?>

function getBaseUrl(baseUrl)

</script>

Then in /js/jquery.hello-lightbox.min i have:

(function($){

function getBaseUrl(baseurl)
{
var domain = baseurl
}

var urrl = 'http://'+domain+'/skin/frontend/default/customtheme/images/lightbox/';

  $.fn.lightBox=function(settings)settings=jQuery.extend({overlayBgColor:'#000',overlayOpacity:0.8,fixedNavigation:false,imageLoading: urrl+'lightbox-ico-loading.gif',imageBtnPrev:urrl+'lightbox-btn-prev.gif', . . . . . . . . . . 

But this doesn't work. In fact it seems like i can't even pass the php variable $baseUrl to var baseUrl in head.phtml

Do you have any ideas?

like image 623
Guille Avatar asked Feb 13 '13 23:02

Guille


2 Answers

There are syntax errors in your main code. I think what you want is to define a function that returns the base URL like so:

<?php $baseUrl = $this->getBaseUrl() ; ?>

<script type="text/javascript">

function getBaseUrl() { return '<?php echo $baseUrl; ?>'; } 

</script>

then use it in JavaScript: (get rid of the function getBaseUrl(baseurl) ... stuff there)

var urrl = 'http://'+getBaseUrl()+'/skin/frontend/default/customtheme/images/lightbox/';
like image 116
Pekka Avatar answered Sep 22 '22 08:09

Pekka


Try to put quotes around the JS var you're setting via the php echo:

var baseUrl = '<?php echo $baseUrl ; ?>'

like image 23
philwinkle Avatar answered Sep 22 '22 08:09

philwinkle