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
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.org/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:
link
tag and append that to the document via JSstyle
tag and append that to the DOM and insert the styles there via AJAX or just have them as a string inside the javascript filehow to add a custom made .js/ .css file to a shopify theme:
for .js:
{{ "filename.js" | asset_url | script_tag }}
<script type="text/javascript">
yourFunction();
</script>
in case the script has async/defer 2 options:
<script src="{{ 'filename.js' | asset_url }}" async></script>
<script src="{{ 'filename.js' | asset_url }}" defer></script>
<script src="//ajax.googleapis.com/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:
{{ "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.com/username/project/master/script.js"></script>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
CSS (Cascading Style Sheets) is a formal language for describing the appearance of a document written using a markup language.
It is mainly used as a means of describing, decorating the appearance of web pages written using HTML and XHTML markup languages, but can also be applied to any XML documents, such as SVG or XUL.
https://www.w3.org/TR/CSS/#css
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.