【发布时间】:2014-03-16 11:50:38
【问题描述】:
我正在构建一个使用Shopify Embedded App SDK 的应用程序。我正在尝试从请求 url 中提取一个参数,并将其传递给嵌入式应用程序的 javascript 初始化函数。但是捕获的变量没有按预期插入。
我可以成功地从请求中捕获参数,但是变量永远不会在初始化函数中输出。
到目前为止,这是我的代码:
<script src="//cdn.shopify.com/s/assets/external/app.js?123445566"></script>
<script type="text/javascript">
//Capture the params from the URL = Works fine
var urlParams;
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
//This is the part I am having troubles with, the urlParams["shop"] just outputs "urlParams["shop"]" instead of the value
ShopifyApp.init({
apiKey: "thiscontainsthecorrectapikeyvalue", // Expects: 32 character API key string like ff9b1d04414785029e066f8fd0465d00
shopOrigin: 'https://' + urlParams["shop"], // Expects: https://exampleshop.myshopify.com
debug: true
});
//This alert works as expected, and shows the correct value: "exampleshop.myshopify.com"
alert(urlParams["shop"]);
</script>
我可以使用普通的 javascript 或 jquery,但请注意,我对两者都不熟悉,主要熟悉 rails(我不能在这个特定页面中使用)。
关于如何将捕获的参数传递给初始化函数的任何想法?
【问题讨论】:
标签: javascript jquery shopify