0

I use javascript bookmarklet with reference to extern path

javascript:(function(){var s = document.createElement('script');s.src = 'http://192.168.0.100/my.js';document.body.appendChild(s);})();

How I can create reference to jQuery.js file that is located in the same directory that my.js file?

Can I attah css file to this files?

3 Answers 3

1
javascript:(function(){
    var s = document.createElement('script');
    s.src = 'http://192.168.0.100/my.js';
    document.body.appendChild(s);
    // Additional js file
    s = document.createElement('script');
    s.src = '<jQuery location goes here>';
    document.body.appendChild(s);
    // CSS file
    s = document.createElement('link');
    s.rel = "stylesheet";
    s.href = "<location goes here>";
    s.type = "text/css";
    document.body.appendChild(s);
})();
Sign up to request clarification or add additional context in comments.

Comments

1

Write the same code as you did for my.js:

javascript: (function() {
    var s = document.createElement('script');
    s.src = 'http://192.168.0.100/my.js';
    document.body.appendChild(s);

    var s2 = document.createElement('script');
    s2.src = 'http://192.168.0.100/jquery.js';
    document.body.appendChild(s2);

    var l = document.createElement('link');
    l.rel = 'stylesheet';
    l.href = 'http://192.168.0.100/foo.css';
    l.type = 'text/css';
    document.body.appendChild(l);
})();

2 Comments

What about adding resources to document.head instead..?
document.head isn't widely supported.
0

I think that it might be quite an overkill for your specific requirement, but I'd like to point out the existence of require.js too , as it might be of interest for people finding this question later.

Briefly, what it does is to provide some facilities for dynamically loading resources in the page, supporting dependencies and namespacing (although you'll still have to wrap your modules so that they don't clog the global namespace).

This way, you can also easily manage dependencies, etc. even in (quite large) applications.

Comments

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.