Skip to content

moonrailgun/jquery.scriptdata.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A light script data plugin

Basic Usage

test.html

<!-- jquery is required -->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../jquery.scriptdata.js"></script>
<script type="text/javascript" src="test.js" data="a=0"></script>

test.js

console.log($.script.getData());
// =>{a: "0"}

Advanced Usage

  • more data
<script type="text/javascript" src="test.js" data="a=0&b=1&c=s&d=2"></script>
console.log($.script.getData());
// =>{a: "0", b: "1", c: "s", d: "2"}
  • more field
<script type="text/javascript" src="test.js" data="a=0&b=1&c=s&d=2" data2="a=3&b=d&c=w&d=e"></script>
console.log($.script.getData());
// =>{a: "0", b: "1", c: "s", d: "2"}
console.log($.script.getData('data2'));
// =>{a: "3", b: "d", c: "w", d: "e"}
  • json string
<meta charset="UTF-8">
<script type="text/javascript" src="test.js" data='{"a":0,"b":1,"c":"hello","d":"你好"}'></script>
console.log($.script.getData());
// =>{a: 0, b: 1, c: "hello", d: "你好"}
  • multiple file
<script type="text/javascript" src="test.js" data='{"a":0}'></script>
<script type="text/javascript" src="test.js" data='{"a":"0"}'></script>
<script type="text/javascript" src="test.js" data='{"a":"s"}'></script>
<script type="text/javascript" src="test.js" data='{"a":""}'></script>
<script type="text/javascript" src="test.js" data='{"a":a}'></script>
console.log($.script.getData());
// =>{a: 0}
// =>{a: "0"}
// =>{a: "s"}
// =>{{"a":a}: undefined}

be sure input is a valid json string!

Use in $(function(){…});

If you want use struct like $(function(){…}); to warp your js logic.I think we have not a good solution now because the nested logic is run when all script have been load.

If have own one script need param.Please put it at the lastest script tag. If have two or more script. Try to set a globar var in js file, outside of $(function(){…}); struct.

I will try to fix it in next version.

About

A light script data plugin

Resources

License

Stars

Watchers

Forks

Packages

No packages published