0

I have this bookmarklet, which follows what was proposed in this topic:

javascript: (function () {  
    var jsCode = document.createElement('script');  
    jsCode.setAttribute('src', 'http://path/to/external/file.js');  
  document.body.appendChild(jsCode);  
 }());

My question is, how can I give the server script some parameter (e.g. page title)? Server script is the following (just some PoC so far):

(function(message){
    alert(message + " " + Math.random()*100);
})();
Community
  • 1
  • 1
Antoine
  • 5,055
  • 11
  • 54
  • 82

1 Answers1

4

To pass a parameter to a server-side script, extend the query string:

    jsCode.setAttribute('src', 'http://path/to/external/file.js?parameter=' + document.title);

If you're intending to pass a parameter to the returned script, add an onload handler to the script element:

javascript:(function() {
    var jsCode = document.createElement('script');  
    var parameter = document.title; //Define parameter
    jsCode.onload = function() {
        // If the function exists, use:
        if(typeof special_func == "function") special_func(parameter);
    };
    jsCode.setAttribute('src', 'http://path/to/external/file.js');
    document.body.appendChild(jsCode);
})();

Response script (file.js):

function special_func(param){
    alert(param);
}

Update / example

If you want to pass (multiple) variable(s) to your response script, use the following code:

javascript:(function() {
    var jsCode = document.createElement('script');  
    var params = ["test", document.title]; //Define parameter(s)
    var thisObject = {}; // `this` inside the callback function will point to
                         // the object as defined here.
    jsCode.onload = function() {
        // If the function exists, call it:
        if (typeof special_func == "function") {
            special_func.apply(thisObject, params);
        }
    };
    jsCode.setAttribute('src', 'http://path/to/external/file.js');
    document.body.appendChild(jsCode);
})();

http://path/to/external/file.js:

// Declare / Define special_func
var special_func = (function(){
    var local_variable = "password";
    var another_local_title = "Expected title";

    //Return a function, which will be stored in the `special_func` variable
    return function(string, title){ //Public function
        if (title == another_local_title) return local_variable;
    }
})();
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Yeah I tried that, but the pop-up always returns "undefined". Even with the parameter name in URL matching the one in the anonymous function. – Antoine Oct 26 '11 at 13:41
  • The function should **NOT** be anonymous. You should include at least one public method at your JavaScript. I'll update my answer with an example. – Rob W Oct 26 '11 at 13:42
  • Thanks, that works ! Although it took me some time to spot the missing ";" in your second sample. It should be added after the jsCode.onload instruction. – Antoine Oct 26 '11 at 15:27
  • @Antoine `public_function should have been `function`. I have improved the answer. – Rob W Oct 26 '11 at 16:00
  • @lukiahas Thanks for the [suggested edit](http://stackoverflow.com/suggested-edits/283295). Unfortunately, it got rejected by the reviewers, so I added it myself. – Rob W Jun 08 '12 at 15:12