-1

I am developing a bookmarklet, that uses a JavaScript file, on cross-domain, and i need to catch the full absolute path of the JavaScript file;

Example, i store my JavaScript file on domain.com and i am accesing my script from domain.org

My JavaScript file is not on domain.org, i want to get this result: domain.com

Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100

1 Answers1

2

you can get the path in the js file it self:

var find_file_path = function (name) {
    var scripts = document.getElementsByTagName('script');

    for (var i = scripts.length - 1; i >= 0; --i) {
        var src = scripts[i].src;
        if (src.indexOf(name) > -1) {
            return src;
        }
    }
    return false;
};

var file_path = find_file_path(THE_NAME_OF_JS_FILE);   

now file_path is the url of the file itself

Siyuan Zhang
  • 405
  • 3
  • 9
  • whats with var scripts = document.getElementsByTagName('script'); ? ... the idea is that you activate the bookmarklet, and you never know the structure of the website that you are on; i dont understand your code – Ionut Flavius Pogacian Jun 19 '12 at 08:05
  • how do you include this js file in your bookmarketlet? do you do it this way? javascript:(function(){ var newScript = document.createElement('script'); newScript.src = JS_FILE_PATH; document.body.appendChild(newScript); })(); – Siyuan Zhang Jun 19 '12 at 08:12
  • not working;`var find_file_path = function (name) { var scripts = document.getElementsByTagName('script'); for (var i = scripts.length - 1; i >= 0; --i) { var src = scripts[i].src; if (src.indexOf(name) > -1) { return src; } } return false; }; var file_path = find_file_path('input.js'); var projectURL = file_path; console.log(project_url);` i get the error : Uncaught ReferenceError: project_url is not defined – Ionut Flavius Pogacian Jun 19 '12 at 08:12
  • yes, because you defined projectURL instead of project_url.. try correct it? – Siyuan Zhang Jun 19 '12 at 08:14
  • btw, my new link is like http://www.cnn.com/home/cnn/Discovery/bookmarklet/input.js?x=0.23214325453, can i get only the http://www.cnn.com/home/cnn/Discovery/bookmarklet/ ? – Ionut Flavius Pogacian Jun 19 '12 at 08:34
  • sure, do it this way: var s = "cnn.com/home/cnn/Discovery/bookmarklet/input.js?x=0.23214325453"; var result = s.split('input.js')[0]; – Siyuan Zhang Jun 19 '12 at 08:50
  • but ofcourse, the split and the explode ... thx for reminding me – Ionut Flavius Pogacian Jun 20 '12 at 08:19