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

Swaraj Chhatre
  • 627
  • 1
  • 10
  • 22
  • This article will help you with invoking a function when you have the name of it. http://stackoverflow.com/questions/359788/how-to-execute-a-javascript-function-when-i-have-its-name-as-a-string – The Muffin Man Oct 28 '13 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. – Pointy Oct 28 '13 at 16:04

2 Answers2

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.

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
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.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 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. – Elliot Bonneville Oct 28 '13 at 16:10
  • @ElliotBonneville: That's possible, but I don't see how that makes any sense at all... – Guffa Oct 28 '13 at 16:14
  • 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. – Elliot Bonneville Oct 28 '13 at 16:17
  • @ElliotBonneville yes I would like to call the function specified in my script tag. – Swaraj Chhatre Oct 28 '13 at 16:18
  • @ElliotBonneville: I took that as debugging code to see if the function was called at all. – Guffa Oct 28 '13 at 16:18
  • @Guffa can you edit the document.write because it's not embedding the script properly. – Swaraj Chhatre Oct 28 '13 at 16:19
  • 1
    @SwarajChhatre: Right, I forgot to interrupt the `script` tags inside the string. Fixed it. – Guffa Oct 28 '13 at 16:21
  • @Guffa it gives x = undefined.It doesn't call the function I have written – Swaraj Chhatre Oct 28 '13 at 16:24
  • @SwarajChhatre: As the code runs without an error, the function is called. You can check the console to see that your message is logged there. The function doesn't return anything (as Elliot Bonneville pointed out), so the value of calling the function is `undefined`. Put something like `return "1234";` in the function, and that value will be used in the source. – Guffa Oct 28 '13 at 16:31
  • @Guffa Actually this is like x taking the return value of function testInLine() but I want the function to be executed real time in the script rs.js. The function would not be as simple as the one I have written.It might not have a written values as well.Hence I need to call the function and not just take its return value. – Swaraj Chhatre Oct 28 '13 at 17:21
  • 1
    @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. – Guffa Oct 28 '13 at 17:50
  • @Guffa The script tag is inserted in the DOM manually.Hence I think any possibility of the function getting executed is negated I guess.One more thing there's a change in function.The function now takes arguments which are variables defined in the src javascript.That is also not possible right. – Swaraj Chhatre Oct 28 '13 at 17:56
  • @SwarajChhatre: This all is a bit too vague and counterintuitive... What is it that you are trying to accomplish, really? And what do you mean when you say that the script tag is inserted manually? – Guffa Oct 28 '13 at 18:35
  • @Guffa My manager sucks.Those were his requirements for which I had to build a prototype for.I made him clear that variables which are accessible after execution on browser cannot be accessed before it being executed.Thanks for your help. – Swaraj Chhatre Oct 29 '13 at 18:07