0

how do i pass parameters into an external jScript using jQ?

i was able to get this working fine:

<script
     id = 'myScript' 
    data-myParm =' my value'
    src = './myScript.js' >
</script>

myScript.js:

document.getElementById('myScript').getAttribute('data-myParm')

but i thought i had seen a very easy (and better) way to do it using jQuery, and not using a script id. also i would prefer not to use anything like

<script src='./myScript.js?myParm=my%value&'> </script>

i tried something like $.data('myParm') and $('script').data('myParm') with no success.

suggestions?

2
  • a very easy (and better) way to do it using jQuery ... using jQuery is not necessarily "better" than using vanilla javascript. Commented Mar 8, 2015 at 19:18
  • maybe jQuery is not necessarily "better" but it is more fun. Commented Mar 8, 2015 at 20:47

2 Answers 2

1

If your scripts are being loaded synchronously, then each script will be the last script in the DOM at the time that it initially runs.

So you should be able to do this:

var param = $('script').last().data('myparm');

or equivalently:

var scripts = document.getElementsByTagName('script');
var param = scripts[scripts.length - 1].getAttribute('data-myparm');

Note that you would do this outside of any document ready handlers or anything that would execute after the script is initially loaded.

Edit: The HTML5 spec says that data-* attributes should be treated as all lowercase, and it looks like jQuery's .data(...) will only work if the provided string is all lowercase, so I suggest using all lowercase data-* attributes throughout.

Sign up to request clarification or add additional context in comments.

3 Comments

$('script').last().data('myparm') worked and thank you - i am not sure why it needed to be lowercased. maybe the data-dash requires it?
@edwardsmarkf Yeah, looks like the HTML5 spec says that data-* attributes should be treated as lowercase (link), so you're probably best off using all lowercase for them in both your HTML and your JS. BTW, the answer you accepted is a blatant copy of mine (you can check the timestamps and edit history if you doubt this). If this did indeed answer your question, would you please accept mine?
Sorry - thought i did accept your answer. your answer did come through first. but either way, this is very exciting to have it working. and thank you again for helping me. for some reason, my question itself was voted down, even though i did research this issue quite a bit, and i do think this question is very relevant considering how few <script> examples there are that actually take advantage of html5 data-dash.
1

Try

var data = $("script").filter(function(i, el) {
   return !!$(el).data().myparm
}).data().myparm || void 0;

console.log(data)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script
    id = 'myScript' 
    data-myParm =' my value'
    src = '' >
</script>

1 Comment

}).data().myparm || void 0; - - - Uncaught TypeError: Cannot read property 'myparm' of undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.