最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to inject custom js to shopify via app - Stack Overflow

programmeradmin5浏览0评论

Hello I'm pretty new to shopify app development. I created the shopify app with laravel and now I generated a js file with content. What I want is to inject this code automatically to the shopify store, so every user which installed the app the code will be added to his store autmatically. I currently read about script tags and probablly is the best way to insert with them, but it's not clear for me. I want information which will help me to figure this out. Thanks in advance

Hello I'm pretty new to shopify app development. I created the shopify app with laravel and now I generated a js file with content. What I want is to inject this code automatically to the shopify store, so every user which installed the app the code will be added to his store autmatically. I currently read about script tags and probablly is the best way to insert with them, but it's not clear for me. I want information which will help me to figure this out. Thanks in advance

Share Improve this question asked Sep 11, 2020 at 7:13 Plamen PenchevPlamen Penchev 4371 gold badge6 silver badges20 bronze badges 2
  • Yes Scripts Tags is the way to go. You just append the styles in the DOM via plain javascript. Can you show us what have you tried since "not clear to me" is to vague? – drip Commented Sep 11, 2020 at 9:47
  • Hello and thanks for your answer I will paste some code to pastebin, you can view it here pastebin.pl/view/022b6d8b , and I don't know how to append this code to shopify after the app is installed? – Plamen Penchev Commented Sep 11, 2020 at 11:15
Add a ment  | 

2 Answers 2

Reset to default 13

Well it's pretty straightforward.

You must create a public URL for the script file first. (so that you can access it from everywhere)

After that you make a post request with the following end point: https://shopify.dev/docs/admin-api/rest/reference/online-store/scripttag#create-2020-07

Where "src": "https://djavaskripped/fancy.js" is your public URL for the the javascript.

This should be done only once when the user is installing their APP.

After that the script will be loaded every time automatically for each theme, there will be no need for additional logic to output it.

When you uninstall the app the script is removed from the admin automatically as well.


Now for the question, how to add your css?

You have two ways:

  • create a public URL for the CSS file and create a link tag and append that to the document via JS
  • create a style tag and append that to the DOM and insert the styles there via AJAX or just have them as a string inside the javascript file

how to add a custom made .js/ .css file to a shopify theme:

  • add your file.js/file.css in the Asset folder

for .js:

  • add the following code in the head part of theme.liquid file
{{ "filename.js" | asset_url | script_tag }}
  • add the following code to the page you need to add the js to
<script type="text/javascript">
        yourFunction();
</script>

in case the script has async/defer 2 options:

  • option 1 local scripts ( stored in Shopify )
<script src="{{ 'filename.js' | asset_url }}" async></script>

<script src="{{ 'filename.js' | asset_url }}" defer></script>
  • option 2 remote scripts
<script src="//ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js" async></script>

in order to use async and defer tags the src attribute must be present documentation here

for .css:

  • add the following code in the head part of theme.liquid file
{{ "filename.css" | asset_url | script_tag }}

NOTES

you can make a repo in Github and load your .js/.css via raw.github

<script src="https://raw.github./username/project/master/script.js"></script>
发布评论

评论列表(0)

  1. 暂无评论