开发者

Importing external json files to a javascript script client-side without external libraries

开发者 https://www.devze.com 2023-01-23 16:34 出处:网络
I\'m a bit new to ja开发者_Go百科vascript.Is there a way to do what I am describing in the title completely client-side and without any external libraries?Or is using jQuery the best/only way to go?Yo

I'm a bit new to ja开发者_Go百科vascript. Is there a way to do what I am describing in the title completely client-side and without any external libraries? Or is using jQuery the best/only way to go?


You can import a json file from a server via AJAX and them simply eval it. You don't need a library for that but using one makes it a lot easier. Of course just evaling a json string is not very secure as it can contain arbitrary text so all libraries parse it to see if it's well formed etc.

EDIT:

If you want to learn about AJAX you can start with this tutorial from w3schools. Ajax stands for Asynchronous Javascript And XML and it allows you to send a request to the server without reloading the whole page. In your case you will not be using Xml but JSON. Anyway, the tutorial explains the whole idea.


Yes there is. You can use the "document.write" to add scripts to the DOM at runtime: in your case:

document.write('<script ...></script>');

Basically you are adding the script tag to the dom that will request the new file. However there is something else to consider, although the script will be downloaded, you will need to have a variable assignment in it in order to use it in your page:

var x = { //json object };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号