Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6984923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:36:45+00:00 2026-05-27T18:36:45+00:00

I want to add a script to a given page with a Greasemonkey style

  • 0

I want to add a script to a given page with a Greasemonkey style user script in Chrome.

I use this trick to add jQuery to my user script.

If I try to add i.e. <h1>Test</h1> to a pages body with jQuery’s append-method, everything works fine.

However if I try to append a JS-script nothing happens.

How do I troubleshoot a problem like this?

This is my .user.js-file:

// ==UserScript==
// @match http://*/*
// ==/UserScript==

// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {

$(document).ready(function() {
    $('body').append('<script type="text/javascript" src="http://timkl.com/wip/bibobRedesign/js/jquery.contenthack.js"></script>');
});

}

// load jQuery and execute the main function
addJQuery(main);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:36:46+00:00Added an answer on May 27, 2026 at 6:36 pm

    jQuery handles script elements differently than other DOM elements. There is additional detail in this answer.

    To append your script the safest way would be to use the same method that is used to insert the jQuery library.

    function main() {
        var script = document.createElement("script");
        script.setAttribute("src", "http://timkl.com/wip/bibobRedesign/js/jquery.contenthack.js");
        document.body.appendChild(script);
        $(document).ready(function () {
            //$('body').append();
        });
    }
    

    Untested Option
    If you have lots of scripts to load take a look at a script loader (such as LABjs)

    You might be able to do something like the following:

    function addLABjs(callback) {
        var script = document.createElement("script");
        script.setAttribute("src", "http://somepath.to/LAB.js");
        script.addEventListener('load', function () {
            var script = document.createElement("script");
            script.textContent = "(" + callback.toString() + ")();";
            document.body.appendChild(script);
        }, false);
        document.body.appendChild(script);
    }
    
    // the guts of this userscript
    
    
    function main() {
        $LAB.script("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js").wait()
            .script("http://timkl.com/wip/bibobRedesign/js/jquery.contenthack.js")
            .script("YourOtherScript.js").wait();
    
        $(document).ready(function () {
        });
    }
    
    addLABjs(main);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script where I want to add an object to an array
This is a crossbrowser window.onload script, I want to add a message, when the
I want to use the script below to add pdf files stored as blob
I want add my custom sidebar next right column all page. Please check this
I want add new Feed item on entity persist and update. I write this
I want add my page some div and another tags.I wanna give unique class
I have a page with an add item button. When this button is clicked,
I want to add and load my scripts in a safe way. However, when
I want add UIGestureRecognizerDelegate to UIWebView,but failed. if [self.view addsubView:webView]; So UIWebView is ok,but
I want add label to every row in tableview at right side of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.