I have included my json file like this
<script type="text/javascript" src="slideshows/1/myfile.json"></script>
And I wonder how I can now create a js object from this file?
If the json was defined inline, or defined in a .js-file it would already have a variable connected to it, but how does it work now that it is in a separate json file?
A JSON file is not a script. It is data.
You need to use a data access method, such as jQuery's $.getJSON() to get it.
Using script tags to import JSON in the page has only on useful use case, which is a way to prevent Cross-origin security issue from happening. We use it as JSONP in JavaScript by sending a callback attribute on the query-string:
<script type="text/javascript"">
window.getMyJSON = function(json){
};
</script>
<script type="text/javascript"
src="http://someotherdomain/slideshows/1/myfile.json?callback=getMyJSON">
</script>
BUT it seems here the JSON file is located just on the current domain, so as #Scimonster has pointed out you can simply achieve it using $.getJSON().
Related
In media/json I have JSON files and I want to read them with a JavaScript file which is in static/js without hardcoding. How can I do it?
Assuming you mean how to get the JSON through an AJAX or similar mechanism which the JavaScript in question will deal with, then you probably want JsonResponse and a simple view that starts at an URL on your server and returns the JSON in question. The script will be loaded in some other view using a <script src="http://wherever" /> tag. In a template you'll probably use <script src="{% static "js/scriptname" %} /> See managing static files
It's not clear what you mean "without hardcoding". The view can receive arguments through its URL and serve up the JSON that these arguments dictate.
I added a JSON file to the assets folder in my Shopify liquid theme. I want to get and parse this JSON object in a jquery method from a javascript file in my assets folder. I've tried including the json file as an asset_url and I've tried using jquery's getJSON() method with the asset's path but the file can't be found. Does anyone know a good approach for adding a custom data object to a shopify liquid theme and the best way to access it?
You could save your JSON in a .liquid file and include it in your template. You'd define the JSON like this:
<script type="text/javascript">
window.my_json_obj = {
...
}
</script>
That way, you could access window.my_json_obj in your jQuery script. But if a key/value storage approach is enough for your needs, you should probably take a look at Shopify's metafields
I have a handlebars template in a public directory. I'd like to access the contents of this file with JavaScript. Here's the code for the script:
<script id="test_template" type="text/template" src="/templates/test.hbs"></script>
If I dump out the contents of the file inside the script tag (instead of using the "src" attribute) I can easily get at the contents, but then I won't get any caching.
Is it possible to get the contents of this file without using an AJAX call?
Is it possible to get the contents of this file without using an AJAX call?
No, you'll need to use an AJAX call.
I have an xml file created in E drive E:\xmlData.xml.
I need to load this xml file into jsp/html file on page load using JavaScript/jQuery.
Generally speaking JavaScript is client-side technology. It doesn't have access to your local file system and you can't load a file from there. But HTML5 provides the File System API. It allows you to solve your issue. Here's an example.
Create a Rest API based server. which will return the xml when the request is given by an Ajax call.
jQuery provides a method $.get which can capture the data from a URL. So to "read" the xml document, it needs to be accessible through a URL.
for using jQuery you must download jQuery javascript library from http://jquery.com/
and put it on the current project/solution file.
use this inside the head tag :
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
This is an example script this will alert xml file content on load...
<script type="text/javascript">
var localFileUrl = '/Yourdirectory/file.xml';
$(document).ready(function() {
$.get(localFileUrl , function(fileContent) {
alert(fileContent);
});
});
</script>
Is it possible to embed <% ... %> tags in a javascript file and have it render the appropriate server-side code? How can this be done?
The process is actually backwards from what you're thinking.
You create an *.aspx page that renders to a JavaScript file. You can then reference that *.aspx page in your <script> tag:
<script type="text/javascript" src="someJavaScript.aspx"></script>
Yes, this is possible, but it would not be a JS file, it would be an aspx file with Javascript in it instead of html.
In your page, to refrence it you would do:
<script type="text/javascript" src="myPage.aspx"></script>
I'm going to make some assumptions on what you are trying to accomplish. Most likely you have a javascript file that needs access to some info on the server. Let's say you need some stuff from the session that if it were an aspx page you'd make it look something like
<script type="text/javascript">
var username = '<%= Session["username"] %>';
var account_num = '<%= Session["account_num"] %>';
</script>
obviously this won't work in a .js file since it never goes through the page lifecycle that an aspx page would be processed though. However, you don't need to transform your entire .js file into an .aspx page as some others might be suggesting. There are lots of other ways to expose that data to your js code. I'll list 2.
emit the above <script> in your page response (perhaps using a <asp:ContentPlaceHolder />)
create a webservice (could even be a simple .ashx) that returns the var username = ... or even better returns json
Yes, it's possible by simply making a regular web page that contains Javascript instead.
However, it might not behave like you expect. Javascript files are cached longer than pages. As the browser might not request the file from the server each time, your could would not be executed each time the file is used.