0
<HTML>
<HEAD>

<TITLE>Your Title Here</TITLE>

</HEAD>
<BODY BGCOLOR="FFFFFF">
<CENTER><IMG SRC="ad.jpeg" ALIGN="BOTTOM"> </CENTER>
<script src="http://b.voicefive.com/c2/15770633/rs.js#c1=3&c3=2816501&c4=21996240&c5=3739279_170437&c6=&c10=&c11=170437&c13=225&c16=adtech&x=testInline()" type="text/javascript">

</script>
<script>
function testInline()
{
    console.log("Hello World");
}
</script>

Now when the query parameters are passed in my javascript function it takes the last parameter x="testInLine()" as a string but I want it to take it as a function which I have written below.The objective is I want to call and execute 3rd party API's by passing them as query parameters. Help is appreciated. Thanks Swaraj

2
  • This article will help you with invoking a function when you have the name of it. stackoverflow.com/questions/359788/… Commented Oct 28, 2013 at 16:02
  • I don't think that other question is really relevant. What's necessary here is to dynamically import the script so that the URL can be constructed from the result of that function call. Commented Oct 28, 2013 at 16:04

2 Answers 2

2

Something like this should do the trick:

var scriptSource = document.getElementsByTagName("script")[0].getAttribute("src");
// safer use document.getElementById("mySourceElement") instead, but you have 
// give your script element the appropriate ID of course
window[scriptSource.substr(scriptSource.indexOf("x="))]();

You will likely want to add a better identifier/selector to your script element, perhaps an ID tag, but this should work, assuming you have only one script element on your page.

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

1 Comment

Can you modify the code for native javascript.I am using javascript.
1

To use a value in the source, create the script tag by writing it from script:

<script type="text/javascript">
document.write('<scr'+'ipt src="http://b.voicefive.com/c2/15770633/rs.js#c1=3&c3=2816501&c4=21996240&c5=3739279_170437&c6=&c10=&c11=170437&c13=225&c16=adtech&x='+ testInline() + '" type="text/javascript"></scr'+'ipt>');
</script>

Remember to declare the testInline function before this script.

14 Comments

I think you have perhaps misinterpreted the (admittedly vague) OP's question. Unless I miss my guess, he'd like to call a function the name of which is specified in his script tag.
@ElliotBonneville: That's possible, but I don't see how that makes any sense at all...
If you'll notice, he's logging "hello world" to the console. It seems to me therefore that he doesn't want to output the result of the testInline function as a string, especially since he isn't actually returning anything useful.
@SwarajChhatre: Right, I forgot to interrupt the script tags inside the string. Fixed it.
@SwarajChhatre: The script file is just plain text until it has loaded and been parsed in the browser, so you can't call it before then. Even if the server could pick up the value that you send in the URL, it can't execute the function in the browser. If you want to call a function in the loaded script, you would just put that call in a script tag right after the script tag that loads it.
|

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.