0

I am loading my script like this:

    var myTestString = "/modules/admin";
    $.ajax({
       type: "GET",
       url: "../../includes/test_tooltip.js",
       data: "module="+myTestString,
       dataType: "script"
     });

Is it possible to have access to module variable in my test_tooltip.js like this?

console.log("I am loaded!" + module);

My scenario is like this. I sometimes need to load that javascript and when i do, i do it in different modules, and i need this javascript to know which module because he will have to do some requests based on the module.

In php i know you can acces data with $_REQUEST["module"], if it could be done in javascript similar would be great.

5
  • what's should respond your test_tooltip.js when you launch your query ? Commented Aug 18, 2011 at 14:13
  • Does this actually have anything to do with PHP? Commented Aug 18, 2011 at 14:28
  • @Awea : test_tooltip.js will have to put some tooltips on some elements, and the text of that tooltips depends on what parameters i send to it. Commented Aug 19, 2011 at 11:26
  • @Ash : It doesn't involve PHP directly, i just need to send some data with that ajax request to the js file, as we do with PHP when making a request. I wished that your code below worked but it doesn't unfortunately. Commented Aug 19, 2011 at 11:27
  • I use window.variable and delete window.variable and case closed. Thanks guys! Commented Aug 19, 2011 at 12:18

1 Answer 1

1
var myTestString = "/modules/admin";
$.ajax({
   type: "GET",
   url: "../../includes/test_tooltip.js",
   context: { module: myTestString },
   dataType: "script"
 });

Your returned javascript should be executed within scope of the context object given to #.ajax, so you could then do:

console.log("I am loaded!" + this.module);
Sign up to request clarification or add additional context in comments.

2 Comments

It doesn't work unfortunately. Any more ideas? have you tested it works for you? maybe i am doing something wrong. It says data not defined.
Okay I've modified the answer with an alternative solution

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.