Can I pre-edit the html of a web page before the javascript is run?

5,030

You might be able to do this by making a userscript that loads the javascript you need in the order you need it. A userscript can execute essentially any arbitrary javascript on the page, including loading other javascript. The only concern would be whether the browser executes the userscript before or after loading the other scripts on the page. This stack overflow question seems to imply (2nd answer) that there's a way to do this.

A simple userscript might use the aforementioned method and the document.write command to insert a new <script> element as needed before the page parses any other javascript.

Share:
5,030

Related videos on Youtube

Daniel Kaplan
Author by

Daniel Kaplan

Updated on September 18, 2022

Comments

  • Daniel Kaplan
    Daniel Kaplan over 1 year

    I'm viewing this website that has a client side javascript error caused by the scripts being loaded in the wrong order. Problem is I don't own this source, but I really need to use the site ASAP.

    Is there a browser/browser plugin that will let me fix this in the browser and let me continue to use the page? The problem is that since this is loaded in the head element, if I modify the html the JS error has already occurred. And if I refresh the changes (at least in chrome) my local modifications have disappeared.

    Specifically, the html is like this:

    <script 
        language="javascript" 
        src="/scripts/util.js" 
        type="text/javascript"></script>
    <script 
        language="javascript" 
        src="/scripts/scriptaculous/prototype.js" 
        type="text/javascript"></script>
    <script 
        language="javascript" 
        src="/scripts/scriptaculous/scriptaculous.js" 
        type="text/javascript"></script>
    

    And to fix the bug, it would need to be like this:

    <script 
        language="javascript" 
        src="/scripts/scriptaculous/prototype.js" 
        type="text/javascript"></script>
    <script 
        language="javascript" 
        src="/scripts/scriptaculous/scriptaculous.js" 
        type="text/javascript"></script>
    <script 
        language="javascript" 
        src="/scripts/util.js" 
        type="text/javascript"></script>
    

    Notice how the util.js moved to the bottom.

  • Daniel Kaplan
    Daniel Kaplan over 10 years
    Thanks for the help. This is the path I'm going down. Is this question better suited for stack overflow?
  • nhinkle
    nhinkle over 10 years
    @tieTYT I think the question is OK for SU since you were initially asking if your browser could do something, and there could be other answers that aren't programming-related. If you run into additional problems along the way, I'd suggest searching on SO first, and then asking a question over there if you need to.
  • Synetech
    Synetech over 10 years
    Unfortunately a user-script won’t work because Chrome doesn’t support document-start for the run-at key, only document-end.
  • nhinkle
    nhinkle over 10 years
    @Synetech hm. Well I'm assuming the user could probably use Firefox just for this one page, but that's a good point.