0

I have this script code:

<script src="http://example.com/embed.js?q=123&parameter1=450&parameter2=300"></script>

How can i get the values of q(123)and parameter1(450) and parameter2(300) and use them into embed.js file? I want to make conditions into my embed.js by using these values. How can i achieve that?

2

4 Answers 4

1

Give the script element and ID attribute like this:

<script id="embed-script" src="http://example.com/embed.js?q=123&parameter1=450&parameter2=300"></script>

Javascript:

var url = document.getElementById('embed-script');

function parseGet(val) {
var result = "",
    tmp = [];
var items = url.src.split("?")[1].split('&');

for (var index = 0; index < items.length; index++) {
    tmp = items[index].split("=");
    if (tmp[0] === val)
        result = decodeURIComponent(tmp[1]);
}

return result;
}

Get the values like this, in embed.js:

var value1 = parseGet('q');

value1 should then be "123".

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

Comments

0

I think you can't,but you can declare all param before required your js file same as:

<script type="text/javascript">
    var q = 123;
    var parameter1 = 450;
    var parameter2 = 300;
  </script>
  <script src="http://example.com/embed.js"></script>

Comments

0

You could place the parameters in attributes of the <script> tag.

<script src="http://example.com/embed.js" q="123" parameter1="450" parameter2="300"></script>

You can access these parameters in embed.js with

document.currentScript.getAttribute('q');
document.currentScript.getAttribute('parameter1');
document.currentScript.getAttribute('parameter2');

Note: document.currentScriptdoes not work on IE.

For more info check this out.

Comments

0

You can access script tag as the last script tag if ask for it without waiting for document load.

~function() {
  var s = document.querySelectorAll("script");
  s = s[s.length-1];
  console.log(s.src);
}();

Comments

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.