javascript 如何通过Javascript获取Magento baseUrl,然后在jquery.hello-lightbox.min中使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14865367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:46:37  来源:igfitidea点击:

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

javascriptmagentobase-url

提问by Guille

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.

我正在尝试通过 head.phtml 文件中的 javascript 获取 Magento BaseUrl,然后在 jquery.hello-lightbox.min 文件中使用它,我需要 baseUrl 来获取一些图像。

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

这是我在 head.phtml 文件中的内容:

<?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:

然后在 /js/jquery.hello-lightbox.min 我有:

(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

但这不起作用。事实上,我什至无法将 php 变量 $baseUrl 传递给 head.phtml 中的 var baseUrl

Do you have any ideas?

你有什么想法?

回答by Pekka

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:

您的主代码中存在语法错误。我认为您想要的是定义一个返回基本 URL 的函数,如下所示:

<?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)

然后在 JavaScript 中使用它:(去掉function getBaseUrl(baseurl) ...那里的东西)

var urrl = 'http://'+getBaseUrl()+'/skin/frontend/default/customtheme/images/lightbox/';

回答by philwinkle

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

尝试在您通过 php echo 设置的 JS var 周围加上引号:

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

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

回答by Rahil

You can call base url via these simple steps throughout the store in every javascript / php file.

您可以在整个商店的每个 javascript / php 文件中通过这些简单的步骤调用基本 url。

Open your theme's page/html/head.phtmland add following code in the HEADtag in the last line:

打开主题的 page/html/head.phtml并在最后一行的HEAD标记中添加以下代码:

<script type="text/javascript">
   var BASE_URL = '<?php echo  Mage::getBaseUrl(); ?>';
</script>

Now you can use BASE_URL variable in every javascript code in your theme files to get magento base url in javascript.

现在,您可以在主题文件中的每个 javascript 代码中使用 BASE_URL 变量来获取 javascript 中的 magento 基本 url。

回答by hammygoonan

If you don't want to use inline Javascript, you can always just add it as an attribute to a div or something along those lines.

如果您不想使用内联 Javascript,您可以随时将其作为属性添加到 div 或类似的内容。

For example, I'll often add a html element like this:

例如,我会经常添加这样的 html 元素:

<div class="my-class" data-storeurl="<?php echo  Mage::getBaseUrl(); ?>">
....
</div>

And then in my Javascript (jQuery in this case), I'll just add something like:

然后在我的 Javascript(在本例中为 jQuery)中,我将添加如下内容:

var current_store = $('.store-redirect').attr('data-storeurl');

It's handy for AJAX calls where you want to run the call on the correct store's url.

这对于 AJAX 调用非常方便,您希望在正确的商店 url 上运行调用。

回答by Francis Kim

EDIT:

编辑:

Javascript won't pass variables between files like that. You don't need to use PHP in this case, just do this:

Javascript 不会像这样在文件之间传递变量。在这种情况下,您不需要使用 PHP,只需执行以下操作:

var urrl = 'http://'+window.location.host+'/skin/frontend/default/customtheme/images/lightbox/';

回答by FAISAL

Magento : Get Base Url , Skin Url , Media Url , Js Url , Store Url and Current Url:

Magento:获取基本网址、皮肤网址、媒体网址、Js 网址、商店网址和当前网址:

  1. Get Base Url :Mage::getBaseUrl();
  2. Get Skin Url :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
    1. Unsecure Skin Url :$this->getSkinUrl('images/imagename.jpg');
    2. Secure Skin Url :$this->getSkinUrl('images/imagename.gif', array('_secure'=>true));
  3. Get Media Url :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
  4. Get Js Url :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
  5. Get Store Url :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
  6. Get Current Url :Mage::helper('core/url')->getCurrentUrl();
  1. 获取基本网址 :Mage::getBaseUrl();
  2. 获取皮肤网址 :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
    1. 不安全的皮肤 URL :$this->getSkinUrl('images/imagename.jpg');
    2. 安全皮肤 URL :$this->getSkinUrl('images/imagename.gif', array('_secure'=>true));
  3. 获取媒体网址 :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
  4. 获取 Js 网址 :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
  5. 获取商店网址 :Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
  6. 获取当前网址 :Mage::helper('core/url')->getCurrentUrl();

Get Url in cms pages or static blocks:

在 cms 页面或静态块中获取 Url:

  1. Get Base Url :{{store url=""}}
  2. Get Skin Url :{{skin url='images/imagename.jpg'}}
  3. Get Media Url `:{{media url='/imagename.jpg'}}
  4. Get Store Url :{{store url='mypage.html'}}
  1. 获取基本网址 :{{store url=""}}
  2. 获取皮肤网址 :{{skin url='images/imagename.jpg'}}
  3. 获取媒体网址`:{{media url='/imagename.jpg'}}
  4. 获取商店网址 :{{store url='mypage.html'}}